作者:LF龙丰通讯 | 来源:互联网 | 2024-09-28 09:28
两种不同方式获取最大值与最小值代码1:#include<stdio.h>intmain(){floatscore[5],
两种不同方式获取最大值与最小值
代码1:
#include
int main()
{
float score[5], sum = 0, max, min;
int i;
printf("请输入5名童鞋的成绩:\n");
for (i = 0; i <5; i = i + 1)
scanf_s("%f", &score[i]);
max = min = score[0];
for (i = 0; i <5; i = i + 1)
{
if (max <= score[i])
max = score[i];
if (min >= score[i])
min = score[i];
}
printf("最高分为:%.1f\n",max);
printf("最低分为:%.1f\n", min);
}
代码2:
#include
int main()
{
float score[5] ;
int i,max, min;
printf("请输入5名童鞋的成绩:\n");
for (i = 0; i <5; i = i + 1)
scanf_s("%f", &score[i]);
max = min = 0;
for (i = 0; i <5; i = i + 1)
{
if (score[max] <= score[i])
max = i;
if (score[min] >= score[i])
min = i;
}
printf("下标为:%d的成绩为最高分:%.1f\n",max,score[max]);
printf("下标为:%d的成绩为最低分:%.1f\n", min,score[min]);
}