测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。
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<iostream>
#include<algorithm>
#include<cstring>
#include<iomanip>
using namespace std;
struct Student{
char name[10];
int score;
int number;
}static buf[100005];
bool cmp1(Student a,Student b)
{
if(a.number!=b.number)
return a.number<=b.number;
else
return 0;
}
bool cmp2(Student a,Student b)
{
int tmp=strcmp(a.name,b.name);
if(tmp!=0) return tmp<0;
else
return a.number<b.number;
}
bool cmp3(Student a,Student b)
{
if(a.score!=b.score) return a.score<b.score;
else
return a.number<b.number;
}
int main()
{
int n,c,i;
int sum=0;
while(cin>>n>>c&&c!=0&&n!=0)
{
sum=sum+1;
for(i=0;i<n;i++)
{
cin>>buf[i].number>>buf[i].name>>buf[i].score;
}
if(c==1)
{
sort(buf,buf+n,cmp1);
cout<<"Case "<<sum<<":"<<endl;
}
if(c==2)
{
sort(buf,buf+n,cmp2);
cout<<"Case "<<sum<<":"<<endl;
}
if(c==3)
{
sort(buf,buf+n,cmp3);
cout<<"Case "<<sum<<":"<<endl;
}
for(i=0;i<n;i++)
{
cout<<setw(6)<<setfill(‘0‘)<<buf[i].number<<" "<<buf[i].name<<" "<<buf[i].score<<endl;
}
}
return 0;
}
原文:http://www.cnblogs.com/xinanliyang/p/5203075.html