用到了strcmp、swap函数
一、介绍
输入:
第一行包含一个正整数N,表示有个人参加了考试。接下来N行,每行有一个字符串和一个正整数,分别表示人名和对应的成绩,用一个空格分隔。
人数<=100,分数<=100,人名仅包含小写字母。
输出:
输出一共有N行,每行一个字符串,第i行的字符串表示成绩从高到低排在第i位的人的名字,若分数一样则按人名的字典序顺序从小到大。
![](https://i-blog.csdnimg.cn/blog_migrate/cd081f7c4bfab3238708d584a6f6307f.png)
二、代码部分
typedef struct与struct定义结构体的区别见【知识笔记】
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct {
char s[100];
int score;
}student;
int n;
int main()
{
student s[100];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i].s >> s[i].score;
}
for (int j = 1; j < n; j++) {
for (int i = 0; i < n - 1; i++) {
if (s[i].score < s[i + 1].score) {
swap(s[i], s[i + 1]);
}
else if (s[i].score == s[i + 1].score) {
int a = strcmp(s[i].s, s[i + 1].s);
if (a > 0)
swap(s[i], s[i + 1]);
}
}
}
for (int i = 0; i < n; i++) {
cout << s[i].s << endl;
}
return 0;
}