输出样例
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
0 0
输出样例
Case 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Case 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Case 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 100010;
struct student{
int num;//定义学号
char name[10];//定义姓名,开多大都行这个区间但别太大
int score;//定义分数
};
bool cmp1(struct student a,struct student b)//自定义比较函数
{
return a.num<b.num;//学号递增
}
bool cmp2(struct student a,struct student b)
{
if(strcmp(a.name,b.name))
return strcmp(a.name,b.name) < 0;
else //姓名首字母相同时按递增学号排列
return cmp1(a,b);
}
bool cmp3(struct student a,struct student b)
{
if(a.score!=b.score)
return a.score < b.score ;//分数不同时按从小到大排序
else
return cmp1(a,b); //分数相同时按递增学号排列
}
int main(){
int n,c,count=0;
struct student stu[maxn];
while(scanf("%d%d",&n,&c)!=EOF)
{
if(n==0)
break;
for(int i=0;i<n;i++)
{
scanf("%d %s %d",&stu[i].num,stu[i].name,&stu[i].score);
}
count++;
switch(c)
{
case 1: printf("Case %d:\n",count); sort(stu,stu+n,cmp1); break;
case 2: printf("Case %d:\n",count); sort(stu,stu+n,cmp2); break;
case 3: printf("Case %d:\n",count); sort(stu,stu+n,cmp3); break;
}
for(int i=0;i<n;i++)
{
printf("%06d %s %d\n",stu[i].num,stu[i].name,stu[i].score);
}
}
return 0;
}
strcmp函数
用法:#include<string.h>
一般形式:strcmp(s1,s2)
说明 当s1<s2时 返回值<0 注意不是-1
当s1=s2时 返回值=0
当s1>s2时 返回值>0 注意不是1