两种不同方式获取最大值与最小值
代码1:
#include <stdio.h> int main() { float score[5], sum = 0, max, min; int i; printf("请输入5名童鞋的成绩: "); 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 ",max); printf("最低分为:%.1f ", min); }
代码2:
#include <stdio.h> int main() { float score[5] ; int i,max, min; printf("请输入5名童鞋的成绩: "); 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 ",max,score[max]); printf("下标为:%d的成绩为最低分:%.1f ", min,score[min]); }