循环结构1(单层for)——这是一道难题

任务描述
编写一个程序,计算一次考试中学生的最高分、最低分和平均分。

输入输出要求
输入: 第一行包含一个整数 n,表示考试人数。接下来 n 行,每行包含一个0到100的整数,表示一个学生的得分。
输出: 三行,第一行输出最高分,第二行输出最低分,第三行输出平均分,平均分保留两位小数。

代码实现
c
#include <stdio.h>

int main() {
    int n;
    scanf(&quot;%d&quot;, &amp;n);
    int maxScore = 0;
    int minScore = 100;
    int totalScore = 0;
    for (int i = 0; i < n; i++) {
        int score;
        scanf(&quot;%d&quot;, &amp;score);
        if (score > maxScore) {
            maxScore = score;
        }
        if (score < minScore) {
            minScore = score;
        }
        totalScore += score;
    }
    double averageScore = (double)totalScore / n;
    printf(&quot;%d\n&quot;, maxScore);
    printf(&quot;%d\n&quot;, minScore);
    printf(&quot;%.2f\n&quot;, averageScore);
    return 0;
}

总结
通过这次练习,我加深了对循环、条件判断和类型转换的理解。同时,我也意识到在编写代码时,清晰的逻辑和良好的代码风格是非常重要的。
全部评论

相关推荐

Aurora23:属于挂一半,暂时进池子了,隔一段时间没有其他组捞的话就彻底结束了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务