P1093 [NOIP2007 普及组] 奖学金
1.题目介绍
[NOIP2007 普及组] 奖学金
题目背景
NOIP2007 普及组 T1
题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前
任务:先根据输入的
注意,在前
7 279
5 279
这两行数据的含义是:总分最高的两个同学的学号依次是
如果你的前两名的输出数据是:
5 279
7 279
则按输出错误处理,不能得分。
输入格式
共
第
第
保证所给的数据都是正确的,不必检验。
输出格式
共
样例 #1
样例输入 #1
6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
样例输出 #1
6 265
4 264
3 258
2 244
1 237
样例 #2
样例输入 #2
8
80 89 89
88 98 78
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
样例输出 #2
8 265
2 264
6 264
1 258
5 258
2.题解
2.1 struct结构体+sort排序
思路
这里我们重写一下sort中的比较函数cmp即可
但是要注意学号小的反而大,这里必须要注意不等号方向!!!
代码
#include <bits/stdc++.h>
using namespace std;
struct Student{
int totalScore;
int chineseScore;
int id;
};
bool cmp(Student a, Student b){
if(a.totalScore != b.totalScore) return a.totalScore > b.totalScore;
if(a.chineseScore != b.chineseScore) return a.chineseScore > b.chineseScore;
return a.id < b.id;
}
int main(){
int n;
cin >> n;
vector<Student> stu(n);
for(int i = 0; i < n; i++){
int chinese, math, english;
cin >> chinese >> math >> english;
stu[i].chineseScore = chinese;
stu[i].totalScore = chinese + math + english;
stu[i].id = i + 1;
}
sort(stu.begin(), stu.end(), cmp);
for(int i = 0; i < 5; i++){
cout << stu[i].id << ' ' << stu[i].totalScore << endl;
}
}
2.2 选择排序
思路
将sort排序部分改为选择排序即可
代码
#include <bits/stdc++.h>
using namespace std;
struct Student{
int totalScore;
int chineseScore;
int id;
};
bool cmp(Student a, Student b){
if(a.totalScore != b.totalScore) return a.totalScore > b.totalScore;
if(a.chineseScore != b.chineseScore) return a.chineseScore > b.chineseScore;
return a.id < b.id;
}
int main(){
int n;
cin >> n;
vector<Student> stu(n);
// 输入
for(int i = 0; i < n; i++){
int chinese, math, english;
cin >> chinese >> math >> english;
stu[i].chineseScore = chinese;
stu[i].totalScore = chinese + math + english;
stu[i].id = i + 1;
}
// 选择排序
vector<Student> ans(5);
for(int i = 0; i < 5; i++){
for(int j = i + 1; j < n; j++){
if(cmp(stu[j], stu[i]) > 0) swap(stu[i], stu[j]);
}
}
// 输出
for(int i = 0; i < 5; i++){
cout << stu[i].id << ' ' << stu[i].totalScore << endl;
}
}
2.3 插入排序
思路
这里由于是要最大的5个,就不是从小到大排序了,所以是从头开始检查更大的。
代码
#include <bits/stdc++.h>
using namespace std;
struct Student{
int totalScore = 0;
int chineseScore = 0;
int id = 300;
};
bool cmp(Student a, Student b){
if(a.totalScore != b.totalScore) return a.totalScore > b.totalScore;
if(a.chineseScore != b.chineseScore) return a.chineseScore > b.chineseScore;
return a.id < b.id;
}
int main(){
int n;
cin >> n;
vector<Student> stu(n);
// 输入
for(int i = 0; i < n; i++){
int chinese, math, english;
cin >> chinese >> math >> english;
stu[i].chineseScore = chinese;
stu[i].totalScore = chinese + math + english;
stu[i].id = i + 1;
}
// 插入排序
vector<Student> ans(5);
for(int i = 0; i < n; i++){
for(int j = 0; j < 5; j++){
if(cmp(stu[i], ans[j]) > 0){
for(int k = 4; k >= j; k--){
ans[k] = ans[k-1];
}
ans[j] = stu[i];
break;
}
}
}
// 输出
for(int i = 0; i < 5; i++){
cout << ans[i].id << ' ' << ans[i].totalScore << endl;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了