现有n名同学的m个测试成绩,第i名同学的编号为i,他的最终成绩是他的所有测试成绩总和,先要挑选最终成绩最高的k名同学去参加比赛,若排名第k的成绩有多个,则全部取。 先输出最终取了多少人,再按顺序输出最终选的同学的编号,相同分数的同学编号从小到大输出。 Input 第一行输入3个数n,m,k(1≤n≤100,1≤m≤10,1≤k≤n)。 接下来n行每行输入m个正整数,代表第i名同学的各个测试成绩(1≤测试成绩≤10)。 Output 先输出最终取的人数,再按顺序输出最终选的同学的编号,相同分数的同学编号从小到大输出。
时间: 2024-04-15 19:25:12 浏览: 17
好的,下面是使用C++编写的解答:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
struct Student {
int id;
int score;
bool operator<(const Student& other) const {
if (score == other.score) {
return id < other.id;
}
return score > other.score;
}
};
int main() {
int n, m, k;
std::cin >> n >> m >> k;
std::vector<Student> students;
for (int i = 0; i < n; i++) {
Student student;
student.id = i + 1;
student.score = 0;
for (int j = 0; j < m; j++) {
int score;
std::cin >> score;
student.score += score;
}
students.push_back(student);
}
std::sort(students.begin(), students.end());
std::cout << k << std::endl;
for (int i = 0; i < k; i++) {
std::cout << students[i].id << " ";
}
return 0;
}
```
这段代码首先读取输入的n、m和k,然后使用一个结构体Student来存储每个同学的编号和最终成绩。接下来,通过循环读取每个同学的测试成绩,并计算最终成绩。将每个同学的信息存储在一个vector中。
然后,使用sort函数对vector进行排序,按照成绩从高到低排序,如果成绩相同,则按照编号从小到大排序。
最后,输出最终取的人数k,以及前k个同学的编号。
希望能帮到你!如果有任何问题,请随时提问。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)