0
点赞
收藏
分享

微信扫一扫

算法题目联系009:成绩排序1

洛茄 2022-03-11 阅读 59
算法

输入用户名和成绩,将成绩按照给出的0或1,判断由高到低排序或由低到高排序,相同成绩都按先录入者排序在前的规则处理

input/output:

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

struct Student{
	string name;
	int score;
}; 

bool comp1(Student a, Student b){
	if(a.score == b.score)
		return &a < &b;
	else
		return a.score<b.score;
}

bool comp0(Student a, Student b){
	if(a.score == b.score)
		return &a < &b;
	else
		return a.score > b.score;
}

int main(){
	int count;
	scanf("%d", &count);
	Student a[count];
	int x;
	scanf("%d", &x);
	for(int i=0; i<count; i++)
		cin>>a[i].name>>a[i].score;
	if(x == 0)
		sort(a, a+count, comp0);		
	else
		sort(a, a+count, comp1);
	cout<<endl;
	for(int j=0; j<count; j++)
		cout<<a[j].name<<" "<<a[j].score<<endl;	
}
举报

相关推荐

0 条评论