
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
float score, max_score = 0, min_score = 100, total_score = 0;
for (int i = 0; i < n; i++) {
scanf("%f", &score);
if (score > max_score) {
max_score = score;
}
if (score < min_score) {
min_score = score;
}
total_score += score;
}
float average_score = total_score / n;
printf("%.2f %.2f %.2f\n", max_score, min_score, average_score);
return 0;
}
-
float score, max_score = 0, min_score = 100, total_score = 0;
:定义浮点型变量score
用于存储成绩,max_score
用于存储最高分(初始化为0),min_score
用于存储最低分(初始化为100),total_score
用于累加总分(初始化为0)。
-
for (int i = 0; i < n; i++) {}
:使用for
循环读取每门课程的成绩,循环次数为输入的科目数。
-
scanf("%f", &score);
:读取输入的每门课程的成绩,并将其存储到变量score
中。 if (score > max_score) { max_score = score; }
:检查是否当前成绩为最高分,如果是则更新max_score
的值。 if (score < min_score) { min_score = score; }
:检查是否当前成绩为最低分,如果是则更新min_score
的值。
-
printf("%.2f %.2f %.2f\n", max_score, min_score, average_score);
:输出最高分、最低分和平均分,其中.2f
表示保留两位小数。