|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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++怎么做
- #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;
- }
复制代码
|
|