ZOJ2727
一道水题 数据小 用效率最低的冒泡都能过
我这是用的c++的sort
注意有个坑的地方:You should output a blank line between two test cases. 也就是最后一组样例输出没有空行
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct stu
{
char name[100];
int year;
int price;
} s[110];
int cmn(stu a,stu b)
{
if(strcmp(a.name,b.name)!=0)
return strcmp(a.name,b.name)<0;
if(a.year!=b.year)
return a.year<b.year;
return a.price<b.price;
}
int cmp(stu a,stu b)
{
if(a.price!=b.price)
return a.price<b.price;
if(strcmp(a.name,b.name)!=0)
return strcmp(a.name,b.name)<0?1:0;
return a.year<b.year;
}
int cmy(stu a,stu b)
{
if(a.year!=b.year)
return a.year<b.year;
if(strcmp(a.name,b.name)!=0)
return strcmp(a.name,b.name)<0?1:0;
return a.price<b.price;
}
int main()
{
int n,i;
char t[10];
scanf("%d",&n);
while(1)
{
if(n==0) break;
for(i=0; i<n; i++)
scanf("%s %d %d",s[i].name,&s[i].year,&s[i].price);
scanf("%s",t);
if(strcmp(t,"Name")==0)
sort(s,s+n,cmn);
else if(strcmp(t,"Price")==0)
sort(s,s+n,cmp);
else
sort(s,s+n,cmy);
for(i=0; i<n; i++)
printf("%s %d %d\n",s[i].name,s[i].year,s[i].price);
scanf("%d",&n);
if(n!=0) printf("\n");
}
return 0;
}