输入一个班全体学生的成绩,把不及格的学生成绩输出,并求及格学生的平均成绩。

输入一个班全体学生的成绩,把不及格的学生成绩输出,并求及格学生的平均成绩。

到底要C还是C++啊

#include <stdio.h>
int main()
{
    float score[100],sum=0;
    int n,count=0;
    printf("输入班级人数:");
    scanf("%d",&n);
    printf("输入所有学生成绩:");
    for(int i=0;i<n;i++)
    {
        scanf("%f",&score[i]);
        if(score[i] > 60)
        {
            sum += score[i];
            count++;
        }
    }
    printf("不及格成绩:\n");
    for(int i=0;i<n;i++)
        if(score[i] < 60)
            printf("%.1f ",score[i]);
    printf("\n");
    if(count == 0)
        printf("没有及格成绩");
    else
        printf("及格成绩平均分:%.1f",sum/count);
}

不需要数组存,直接算

#include <stdio.h>

int main() {
    int n, sum = 0, count = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int score;
        scanf("%d", &score);
        if (score < 60) {
            printf("%d ", score);
        } else {
            sum += score;
            count++;
        }
    }
    printf("\n平均成绩为:%.2f", (float)sum / count);
    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;   // 输入学生人数n
    
    int scores[n];   // 存储n个学生成绩的数组
    int sum = 0;  // 及格学生成绩总和
    int cnt = 0;   // 及格学生人数
    
    for (int i = 0; i < n; i++) {
        cin >> scores[i];  // 输入每个学生的成绩
        if (scores[i] < 60) {   // 如果成绩不及格
            cout << scores[i] << " ";   // 输出成绩
        } else {
            sum += scores[i]; // 总和加上及格成绩
            cnt++;   // 及格学生人数加1
        }
    }
    
    cout << endl;
    if (cnt > 0) {   // 如果有及格学生
        cout << (double)sum / cnt;   // 输出及格学生平均成绩
    } 
}

我来补个C++版:

#include <iostream>
#include <vector>

int main() {
    int numStudents;
    std::cout << "请输入学生人数:";
    std::cin >> numStudents;

    std::vector<int> scores(numStudents);
    int sum = 0;
    int numPass = 0;

    for (int i = 0; i < numStudents; i++) {
        std::cout << "请输入第" << i + 1 << "个学生的成绩:";
        std::cin >> scores[i];
        sum += scores[i];
        if (scores[i] >= 60) {
            numPass++;
        }
    }

    std::cout << "不及格学生的成绩为:";
    for (int i = 0; i < numStudents; i++) {
        if (scores[i] < 60) {
            std::cout << scores[i] << " ";
        }
    }

    double avgPass = static_cast<double>(sum) / numPass;
    std::cout << "\n及格学生的平均成绩为:" << avgPass << std::endl;

    return 0;
}

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7587368
  • 这篇博客也不错, 你可以看下编写一个学生类,学生信息包含姓名、学号、年龄、性别和成绩;(1)输出每个学生的姓名、学号和成绩。(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩和最低成绩。
  • 除此之外, 这篇博客: 学习-输入5个学生的数学、语文、英语3门课程的成绩,计算并输出每一门课程的平均成绩和每一位学生的平均成绩中的 第1关:输入5个学生的数学、语文、英语3门课程的成绩,计算并输出每一门课程的平均成绩和每一位学生的平均成绩 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 任务描述
    本关任务:输入5 个学生的数学、语文、英语 3 门课程的成绩,计算并输出每一门课程的平均成绩和每一位学生的平均成绩。

    相关知识
    定义一个5行3列的二维数组,存入5个学生的数学、语文、英语3门课程的成绩,计算每一位学生的平均成绩,实际是将二维数组按行求总分后再除以课程门数,计算每一门课程的平均成绩实际是将二维数组按列求和再除以人数。

    编程要求
    根据提示,在右侧编辑器补充代码。

    测试说明
    平台会对你编写的代码进行测试:

    测试输入:
    85 78 88 60 90 80 91 79 92 50 84 83 45 86 80
    预期输出:
    每个学生的平均分:
    85 78 88 83.7
    60 90 80 76.7
    91 79 92 87.3
    50 84 83 72.3
    45 86 80 70.3
    每门课的平均分:
    66.2 83.4 84.6

    输入格式:
    输入15个整数,用空格分隔。

    输出格式:
    从第二行起输出5个学生3门课成绩以及平均分,平均分按%.1f格式输出,每个数据间用\t隔开;
    第八行输出3门课的平均分,平均分按%.1f格式输出。

    开始你的任务吧,祝你成功!

    #include<stdio.h>
    #define M  5
    #define N  3
    int main()
    {
    	/*********Begin*********/
    	int a[M][N],i,j;
    	float sum;
        for (i=0;i<M;i++)
    		for (j=0;j<N;j++)
    			scanf ("%d",&a[i][j]);
        printf ("每个学生的平均分:\n");
    	for (i=0;i<M;i++)
    	{
    		sum=0;
    		for (j=0;j<N;j++)
    		{
    			printf ("%d\t",a[i][j]);
    			sum+=a[i][j];
    		}
    		printf ("%.1f\n",sum/N);
    	}
    	printf ("每门课的平均分:\n");
    	for (j=0;j<N;j++)
    	{
    		sum=0;
    		for (i=0;i<M;i++)
    		{
    			sum+=a[i][j];
    		}
    		printf ("%.1f\t",sum/M);
    	}
    	
    	/*********End**********/ 
    	return 0;	
    }
    
    
  • 您还可以看一下 李立宗老师的讲给入门者的深度学习课程中的 有监督学习小节, 巩固相关知识点