题解 | #统计成绩#
统计成绩
https://www.nowcoder.com/practice/cad8d946adf64ab3b17a555d68dc0bba
#include <stdio.h> #include <stdlib.h> #include <float.h> int main() { unsigned short n = 0; float *transcript = NULL; // 在 C 语言中,float 类型有一个特殊的宏表示其最大值,即 FLT_MAX。(需要添加头文件:float.h) float maxScore = 0.0, minScore = FLT_MAX, totalScore = 0.0; scanf("%hu", &n); // 动态申请内存 transcript = (float *)calloc(n, sizeof(float)); if (!transcript) return -1; // 输入/获取n科成绩(范围0.0~100.0),并获取最高分,最低分以及总成绩 for (unsigned short i = 0; i < n; i++) { scanf("%f", &transcript[i]); if (maxScore < transcript[i]) maxScore = transcript[i]; if (minScore > transcript[i]) minScore = transcript[i]; totalScore += transcript[i]; } // 输出最高分,最低分以及平均分 printf("%.2f %.2f %.2f\n", maxScore, minScore, totalScore / n); // 释放内存 free(transcript); transcript = NULL; return 0; }