成绩单排序

Sample Input 1
3 1
000007 James 85
000010 Amy 90

000001 Zoe 60

 

Sample Output 1
000001 Zoe 60
000007 James 85

000010 Amy 90

注意:输入N,C。C=1时按准考证号从小到大排序;C=2时按姓名字典序从小到大排序,若姓名相同,按准考证号从小到大;C=3时分数从小到大排序,若分数相同,按准考证号排序。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
 
const int maxn = 10001;
struct Student
{
	int id;
	char name[10];
	int score;
}stu[maxn];

bool cmp1(Student a, Student b)
{
	return a.id < b.id;
}
bool cmp2(Student a, Student b)
{
	int s = strcmp(a.name, b.name);
	if (s != 0)  return s < 0;  //按字典序从小到大
	else return a.id < b.id;
}
bool cmp3(Student a, Student b)
{
	if (a.score != b.score)  return a.score < b.score;   //按分数大小
	else   return a.id < b.id;
}
int main() {
       	
	int n, c;
	cin >> n >> c;
	for (int i = 0; i < n; i++)
	{
		cin >> stu[i].id >> stu[i].name >> stu[i].score;
	}
	if (c == 1)  sort(stu, stu + n, cmp1);
	else if (c == 2)  sort(stu, stu + n, cmp2);
	else  sort(stu, stu + n, cmp3);

	for (int i = 0; i < n; i++)
		cout << stu[i].id << stu[i].name << stu[i].score;
	 
	return 0;
}
/*  扩展:输出指定分数段内的考生姓名以及准考号
 ... cin>>left>>right;
     sort(stu,stu+n,cmp);
	 bool flag = false;
	 for(int i=0;i<n;i++)
	 {
	    if(stu[i].grade >= left && stu[i].grade <= right)
		{
		  cout<<stu[i].name<<stu[i].id;
		  flag = true;
		}
	 }
	 if(flag == false)   cout<<"NONE"<<endl;
*/


 

 

 

posted @ 2018-08-05 17:36  道微真理  阅读(222)  评论(0)    收藏  举报