鱼C论坛

 找回密码
 立即注册
查看: 911|回复: 1

[已解决]求助一下

[复制链接]
发表于 2023-11-11 16:19:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本题目要求先输入正整数N,然后输入N个类型为结构体stud的数组元素,计算每个学生的总分,输出每个学生的学号、姓名、三门课的成绩及总分;计算全部成绩的平均分并输出;输出总分最高同学的各项信息。
struct stud {
int num; //学号
char name[10]; //姓名
int score[3]; //3门课成绩
int sum; //总分
};

输入格式:
先输入不超过10的一个正整数N,然后每行输入一个学生的信息(学号、姓名、三门课成绩)。学号在整数范围内,姓名长度小于10个字符。

输出格式:
首先输出每个学生的信息(包括学号、姓名、三门课成绩、总分),数据项之间空1格,每人一行;再输出全部成绩的平均分;最后输出总分最高(假设没有相同总分)同学的学号、姓名、三门课成绩及总分,数据项之间空1格。

输入样例:
在这里给出一组输入。例如:

4
1  张三  81  85  82
2  李四  82  78  74
3  王五  85  74  90
4  赵六  77  85  79
输出样例:
在这里给出相应的输出。例如:

1 张三 81 85 82 248
2 李四 82 78 74 234
3 王五 85 74 90 249
4 赵六 77 85 79 241
总平均分=81.000000
3 王五 85 74 90 249

这题用C或者C++怎么做
最佳答案
2023-11-11 16:19:56
#include <iostream>
#include <iomanip>
using namespace std;

struct stud {
    int num; //学号
    char name[10]; //姓名
    int score[3]; //3门课成绩
    int sum; //总分
};

int main() {
    int N;
    cin >> N;
    stud students[N];
    
    for (int i = 0; i < N; i++) {
        cin >> students[ i].num >> students[ i].name >> students[ i].score[0] >> students[ i].score[1] >> students[ i].score[2];
        students[ i].sum = students[ i].score[0] + students[ i].score[1] + students[ i].score[2];
        cout << students[ i].num << " " << students[ i].name << " " << students[ i].score[0] << " " << students[ i].score[1] << " " << students[ i].score[2] << " " << students[ i].sum << endl;
    }
    
    double totalAvg = 0;
    for (int i = 0; i < N; i++) {
        totalAvg += students[ i].sum;
    }
    totalAvg /= N;
    cout << fixed << setprecision(6) << "总平均分=" << totalAvg << endl;
    
    int maxIndex = 0;
    for (int i = 1; i < N; i++) {
        if (students[ i].sum > students[maxIndex].sum) {
            maxIndex = i;
        }
    }
    cout << students[maxIndex].num << " " << students[maxIndex].name << " " << students[maxIndex].score[0] << " " << students[maxIndex].score[1] << " " << students[maxIndex].score[2] << " " << students[maxIndex].sum << endl;
    
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-11 16:19:56 | 显示全部楼层    本楼为最佳答案   
#include <iostream>
#include <iomanip>
using namespace std;

struct stud {
    int num; //学号
    char name[10]; //姓名
    int score[3]; //3门课成绩
    int sum; //总分
};

int main() {
    int N;
    cin >> N;
    stud students[N];
    
    for (int i = 0; i < N; i++) {
        cin >> students[ i].num >> students[ i].name >> students[ i].score[0] >> students[ i].score[1] >> students[ i].score[2];
        students[ i].sum = students[ i].score[0] + students[ i].score[1] + students[ i].score[2];
        cout << students[ i].num << " " << students[ i].name << " " << students[ i].score[0] << " " << students[ i].score[1] << " " << students[ i].score[2] << " " << students[ i].sum << endl;
    }
    
    double totalAvg = 0;
    for (int i = 0; i < N; i++) {
        totalAvg += students[ i].sum;
    }
    totalAvg /= N;
    cout << fixed << setprecision(6) << "总平均分=" << totalAvg << endl;
    
    int maxIndex = 0;
    for (int i = 1; i < N; i++) {
        if (students[ i].sum > students[maxIndex].sum) {
            maxIndex = i;
        }
    }
    cout << students[maxIndex].num << " " << students[maxIndex].name << " " << students[maxIndex].score[0] << " " << students[maxIndex].score[1] << " " << students[maxIndex].score[2] << " " << students[maxIndex].sum << endl;
    
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 11:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表