- 问答
hm4201求解
- @ 2025-12-24 20:42:53
题目描述 在一次考试中,每个考生的成绩都不相同,要求输入每个考生的学号和成绩,求考第n名同学的学号和成绩。
输入 输入学生人数为a(2 <= a <= 10),输入每名学生学号为1、2、3...以及对应学生的成绩,n为需要查找的考第n名同学的名次
输出 输出包含查找的序号以及成绩
样例 【输入样例1】 5 1 99 2 95 3 94 4 98 5 100 1
【输出样例1】 5 100
6 条评论
-
-
#include #include #include
// 定义学生结构体,存储学号和成绩 struct Student { int id; // 学号 int score; // 成绩 };
// 自定义排序规则:按成绩降序排列 bool compare(const Student& s1, const Student& s2) { return s1.score > s2.score; }
int main() { int a; // 学生人数 std::cin >> a;
// 存储所有学生信息 std::vector<Student> students; for (int i = 0; i < a; ++i) { int id, score; std::cin >> id >> score; students.push_back({id, score}); } int n; // 要查找的名次 std::cin >> n; // 按成绩降序排序 std::sort(students.begin(), students.end(), compare); // 输出第n名的学号和成绩(数组下标为n-1) std::cout << students[n-1].id << " " << students[n-1].score << std::endl; return 0;}
-
#include #include #include
// 定义学生结构体,存储学号和成绩 struct Student { int id; // 学号 int score; // 成绩 };
// 自定义排序规则:按成绩降序排列 bool compare(const Student& s1, const Student& s2) { return s1.score > s2.score; }
int main() { int a; // 学生人数 std::cin >> a;
// 存储所有学生信息 std::vector<Student> students; for (int i = 0; i < a; ++i) { int id, score; std::cin >> id >> score; students.push_back({id, score}); } int n; // 要查找的名次 std::cin >> n; // 按成绩降序排序 std::sort(students.begin(), students.end(), compare); // 输出第n名的学号和成绩(数组下标为n-1) std::cout << students[n-1].id << " " << students[n-1].score << std::endl; return 0;}
-
#include<bits/stdc++.h> using namespace std; struct node{ int hao; int fen; }; bool cmp(node a,node b){ return a.fen>b.fen; } int main(){ int n; cin>>n; node a[1005]; for(int i=1;i<=n;i++){ cin>>a[i].hao; cin>>a[i].fen; } sort(a+1,a+1+n,cmp); int v; cin>>v; cout<<a[v].hao<<" "<<a[v].fen; }
- 1