【九度OJ】题目1196:成绩排序 解题报告

时间:2023-03-08 19:29:56
【九度OJ】题目1196:成绩排序 解题报告

【九度OJ】题目1196:成绩排序 解题报告

标签(空格分隔): 九度OJ


http://ac.jobdu.com/problem.php?pid=1196

题目描述:

用一维数组存储学号和成绩,然后,按成绩排序输出。
  

输入:

输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。

输出:

按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。

样例输入:

3
1 90
2 87
3 92

样例输出:

2 87
1 90
3 92

Ways

这个是老生常谈的排序题了,除了拼错了一个student和忘记\n以外,是能A的。

#include<stdio.h>
#include<algorithm> using namespace std; struct Student {
int num;
int score;
} student[110]; bool cmp(Student a, Student b) {
if (a.score != b.score) {
return a.score < b.score;
} else {
return a.num < b.num;
}
} int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d%d", &student[i].num, &student[i].score);
}
sort(student, student + n, cmp);
for (int i = 0; i < n; i++) {
printf("%d %d\n", student[i].num, student[i].score);//别忘回车
}
}
return 0;
}

Date

2017 年 3 月 19 日