http://acm.hdu.edu.cn/showproblem.php?pid=1862
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
struct student{
int node;
string name;
int score;
};
bool cmp1(student a,student b)
{
return a.node<b.node;
}
bool cmp2(student a,student b)
{
if(a.name==b.name)
return a.node<b.node;
else
return a.name<b.name;
}
bool cmp3(student a,student b)
{
if(a.score==b.score)
return a.node<b.node;
return a.score<b.score;
}
student s[100001];
int main()
{
int n,c,k=1;
while(cin>>n>>c,n)
{
for(int i=0;i<n;i++)
{
cin>>s[i].node>>s[i].name>>s[i].score;
}
switch(c)
{
case 1:
{
sort(s,s+n,cmp1);
break;
}
case 2:
{
sort(s,s+n,cmp2);
break;
}
case 3:
{
sort(s,s+n,cmp3);
break;
}
}
cout<<"Case "<<k++<<":"<<endl;
for(int i=0;i<n;i++)
{
cout<<setw(6)<<setfill('0')<<s[i].node<<" "<<s[i].name<<" "<<s[i].score<<endl;
}
}
return 0;
}
这里有问题 提交不了 下面是ac代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
//#include <cmath>
using namespace std;
struct Student
{
char name[20];
char num[20];
int score;
} stu[100005];
bool cmp1(Student x,Student y)
{
if( strcmp(x.num , y.num) > 0 )
return 0;
else
return 1;
}
bool cmp2(Student x,Student y)
{
if( strcmp(x.name , y.name) > 0 )
return 0;
else if(strcmp(x.name , y.name) == 0)
{
if( strcmp(x.num , y.num) > 0 )
return 0;
}
return 1;
}
bool cmp3(Student x,Student y)
{
if( x.score>y.score )
return 0;
else if(x.score==y.score)
{
if( strcmp(x.num , y.num) > 0 )
return 0;
}
return 1;
}
int main()
{
int n,c,i,cnt = 1;
while(cin >> n >> c && n != 0 && c != 0)
{
for(i = 0; i<n; i++)
scanf("%s%s%d",stu[i].num,stu[i].name,&stu[i].score);
if(c == 1)
sort(stu,stu+n,cmp1);
else if(c == 2)
sort(stu,stu+n,cmp2);
else if (c == 3)
sort(stu,stu+n,cmp3);
printf("Case %d:\n",cnt++);
for(i = 0; i<n; i++)
printf("%.6s %s %d\n",stu[i].num,stu[i].name,stu[i].score);
}
return 0;
}