c++描述: 按照输入输出格式要求,根据程序模板将代码补充完整。求出Student数组中学生的最高平均分和最低平均分,并从高分到低分对数组重新排序,输出排序后的学生信息。 输入 先输入3个课程号,然后逐次输入4位学生的信息。 输出 按照格式输出最高分,最低分,以及排序后的结果。 示例: 输入: 201 202 203 1 zhang 67 88 92 2 li 44 77 99 3 wang 76 82 95 4 zhao 63 82 86 输出: Student wang got the highest average score as 84.3333 Student li got the lowest average score as 73.3333 Student id:3 Student name:wang lesson_id 201 202 203 Average scores 76 82 95 84.3333 The student didn't fail. ------华丽的分割线-------- Student id:1 Student name:zhang lesson_id 201 202 203 Average scores 67 88 92 82.3333 The student didn't fail. ------华丽的分割线-------- Student id:4 Student name:zhao lesson_id 201 202 203 Average scores 63 82 86 77 The student didn't fail. ------华丽的分割线-------- Student id:2 Student name:li lesson_id 201 202 203 Average scores 44 77 99 73.3333 The student failed. ------华丽的分割线--------
时间: 2024-02-10 15:32:19 浏览: 107
以下是完整的C++代码:
```c++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct Student{
int id;
string name;
int score[3];
double average;
bool fail;
}stu[4];
bool cmp(Student a, Student b){
return a.average > b.average;
}
int main(){
int lesson[3];
for(int i=0; i<3; i++){
cin >> lesson[i];
}
for(int i=0; i<4; i++){
cin >> stu[i].id >> stu[i].name >> stu[i].score[0] >> stu[i].score[1] >> stu[i].score[2];
stu[i].average = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
if(stu[i].average < 60){
stu[i].fail = true;
}
else{
stu[i].fail = false;
}
}
double max_avg = -1, min_avg = 101;
int max_pos = -1, min_pos = -1;
for(int i=0; i<4; i++){
if(stu[i].average > max_avg){
max_avg = stu[i].average;
max_pos = i;
}
if(stu[i].average < min_avg){
min_avg = stu[i].average;
min_pos = i;
}
}
cout << "Student " << stu[max_pos].name << " got the highest average score as " << fixed << setprecision(4) << max_avg << endl;
cout << "Student " << stu[min_pos].name << " got the lowest average score as " << fixed << setprecision(4) << min_avg << endl;
sort(stu, stu+4, cmp);
for(int i=0; i<4; i++){
cout << "Student id:" << stu[i].id << " Student name:" << stu[i].name << endl;
cout << "lesson_id " << lesson[0] << " " << lesson[1] << " " << lesson[2] << " " << "Average" << endl;
cout << "scores " << stu[i].score[0] << " " << stu[i].score[1] << " " << stu[i].score[2] << " " << fixed << setprecision(4) << stu[i].average << endl;
if(stu[i].fail){
cout << "The student failed." << endl;
}
else{
cout << "The student didn't fail." << endl;
}
cout << "------华丽的分割线--------" << endl;
}
return 0;
}
```
代码思路:
1. 定义结构体 `Student`,存储学生的信息;
2. 读入三门课的课程号;
3. 逐次读入四位学生的信息,计算出平均分,并判断是否不及格;
4. 找出最高平均分和最低平均分的学生位置;
5. 对学生数组进行排序,按照平均分从高到低排序;
6. 按照输出格式输出最高分、最低分和排序后的结果。
注意事项:
1. 使用 `setprecision()` 函数控制浮点数的精度;
2. 本题中的分隔符是中文的“华丽的分割线”,需要注意输出时的中英文符号混用问题。
阅读全文