给定N个学生的基本信息,包括学号(由12个数字组成的字符串)、姓名(长度小于20的不包含空白字符的非空字符串)、高等数学成绩、C语言程序设计成绩,成绩均为[0,100]区间内的整数,要求根据C语言的成绩,对学生名单进行降序排列。 输入格式: 输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 高等数学成绩 C语言程序设计成绩”,中间以空格分隔。 输出格式: 按照C语言课程程序降序,输出所有学生信息,一行是一个学生的信息,信息之间用空格隔开。
时间: 2024-02-29 18:56:53 浏览: 25
非常抱歉,我之前的回答有误,请允许我重新回答你的问题。
以下是正确的代码:
```c++
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10;
struct Student{
string id;
string name;
int math;
int c;
}stu[N];
bool cmp(Student a, Student b){
return a.c > b.c;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> stu[i].id >> stu[i].name >> stu[i].math >> stu[i].c;
}
sort(stu, stu+n, cmp);
for(int i = 0; i < n; i++){
cout << stu[i].id << " " << stu[i].name << " " << stu[i].math << " " << stu[i].c << endl;
}
return 0;
}
```
我们依然使用结构体 `Student` 来存储每个学生的信息,但是这次不再计算每个学生的总分数,因为题目要求只按照 C 语言成绩进行排序。同时,我们还需要将排序规则改为按照 C 语言成绩降序排列。
最后,我们输出排序后的学生信息即可。
希望这次的回答能够满足你的要求,如果还有其他问题,请继续提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)