清华大学 上机复试 成绩排序 简单排序

版权声明:那个,最起码帮我加点人气吧,署个名总行吧 https://blog.csdn.net/qq_41670466/article/details/84573616

赛题网站:https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1?tpId=40&tqId=21333&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

思路:就是简单的用两个自定义比较函数,还有一点就是输入是多组输入,要while(scanf()!=EOF)

#include<bits/stdc++.h>
using namespace std;

struct St
{
		int sco;
		int place;
		char name[20];
}stu[200];

bool cmp1(St a, St b)
{
		if (a.sco == b.sco)	return a.place < b.place;
		return a.sco < b.sco;
}

bool cmp2(St a, St b)
{
		if (a.sco == b.sco)		return a.place < b.place;
		return a.sco > b.sco;
}

int main()
{
		int n, m;
		while (scanf("%d%d", &n, &m) != EOF)
		{
				for (int i = 0; i < n; i++)
				{
						scanf("%s %d", &stu[i].name, &stu[i].sco);
						stu[i].place = i + 1;
				}
				if (m == 0)		sort(stu, stu + n, cmp2);
				else sort(stu, stu + n, cmp1);
				for (int i = 0; i < n; i++)
						printf("%s %d\n", stu[i].name, stu[i].sco);
		}
		//system("pause");
		return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41670466/article/details/84573616