用c++实现给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。 Input 第一行为n (0 < n < 20),表示班里的学生数目; 接下来的n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过20,成绩为一个不大于100的非负整数。 Output 把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。 Sample Input 4
时间: 2024-02-24 16:54:08 浏览: 37
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
int score;
bool operator < (const Student& other) const {
if (score != other.score) {
return score > other.score;
} else {
return name < other.name;
}
}
};
int main() {
int n;
cin >> n;
Student students[n];
for (int i = 0; i < n; i++) {
cin >> students[i].name >> students[i].score;
}
sort(students, students + n);
for (int i = 0; i < n; i++) {
cout << students[i].name << " " << students[i].score << endl;
}
return 0;
}
```
相关问题
用c++的结构体数据解决下面问题:某班有5个学生,参加期末高数、英语、C三门课程考试,请按照学生成绩总分从高到低排序。
可以使用结构体来存储学生信息(学号、姓名、三门课程成绩及总分),然后使用冒泡排序或快速排序等算法对学生信息进行排序,最终输出排序后的学生信息。
以下是使用结构体和冒泡排序实现的代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义学生结构体
struct Student {
string id; // 学号
string name; // 姓名
int math; // 高数成绩
int english; // 英语成绩
int c_programming;// C语言成绩
int total_score; // 总分
};
int main() {
// 定义5个学生
Student students[5] = {
{"001", "张三", 80, 75, 90, 0},
{"002", "李四", 85, 90, 70, 0},
{"003", "王五", 90, 80, 85, 0},
{"004", "赵六", 75, 85, 80, 0},
{"005", "钱七", 70, 90, 75, 0}
};
// 计算每个学生的总分
for (int i = 0; i < 5; i++) {
students[i].total_score = students[i].math + students[i].english + students[i].c_programming;
}
// 冒泡排序
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
if (students[j].total_score < students[j + 1].total_score) {
// 交换两个学生的位置
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// 输出排序后的学生信息
cout << "按照总分从高到低排序的学生信息如下:" << endl;
for (int i = 0; i < 5; i++) {
cout << students[i].id << "\t" << students[i].name << "\t"
<< students[i].math << "\t" << students[i].english << "\t"
<< students[i].c_programming << "\t" << students[i].total_score << endl;
}
return 0;
}
```
输出结果:
```
按照总分从高到低排序的学生信息如下:
003 王五 90 80 85 255
002 李四 85 90 70 245
001 张三 80 75 90 245
004 赵六 75 85 80 240
005 钱七 70 90 75 235
```
给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。 输入格式: 第一行为n (3< n < 200),表示班里的学生数目; 接下来的n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过20,成绩为一个不大于100的非负整数。 输出格式: 把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。
这道题可以使用结构体数组来存储每个学生的姓名和成绩,然后按照成绩从高到低进行排序输出即可。
具体实现如下:
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int N = 210;
struct Student {
string name;
int score;
// 重载小于操作符,用于排序
bool operator< (const Student& other) const {
if (score != other.score) return score > other.score;
return name < other.name;
}
}students[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> students[i].name >> students[i].score;
}
// 对学生数组进行排序
sort(students, students + n);
// 输出排序后的结果
for (int i = 0; i < n; i++) {
cout << students[i].name << " " << students[i].score << endl;
}
return 0;
}
```
在结构体 `Student` 中,我们重载了小于操作符 `operator<`,用于排序。如果两个学生的成绩不同,就按照成绩从大到小排序;如果两个学生的成绩相同,就按照姓名字典序从小到大排序。
然后我们使用 `sort` 函数对结构体数组 `students` 进行排序,排序后输出即可。
时间复杂度为 $O(n \log n)$。