Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
Each string will not exceed 20 characters.
Sure that same origin will not exist the same weapon.
Output
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
Sample Input
5 knife qizhou so-so gun qizhou wonderful knife zhengzhou good stick zhengzhou good rope shengzhou so-so
Sample Output
Case 1
qizhou:
gun wonderful
knife so-so
shengzhou:
rope so-so
zhengzhou:
knife good
stick good
题意就是一个排序分三个步骤: 第一按武器出发点 字典序 排序 ,第二,同一出发点按威力递减排序,第三,前面均相同的按名字字典序排序。
其中第二个排序须注意,我用的是在"goog” 前加上 't' ,组成"tgood",再用字典序排序。
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
struct weapon
{
char name[25];
char origin[25];
char level[25];
} W[505];
// wonderful
// tgood
// so-so
int cmp(const void *a ,const void *b)
{
weapon * x=(weapon * )a,*y=(weapon *) b;
if(strcmp(x->origin,y->origin))
return strcmp(x->origin,y->origin);
else
{
int flag1=1,flag2=1;
if(x->level[1]=='g')
{
x->level[0]='t';
flag1=0;
}
if(y->level[1]=='g')
{
y->level[0]='t';
flag2=0;
}
if(y->level[flag2] - x->level[flag1])
return y->level[flag2] - x->level[flag1];
else
return strcmp(x->name,y->name);
}
}
int main()
{
int N,cnt=0;
while(~scanf("%d",&N))
{
for(int i=0;i<N;++i)
scanf("%s%s%s",W[i].name,W[i].origin,W[i].level+1);
qsort(W,N,sizeof(W[0]),cmp);
printf("Case %d\n",++cnt);
if(N!=0)
{
printf("%s:\n",W[0].origin);
printf(" %s %s\n",W[0].name,W[0].level+1);
for(int i=1;i<N;++i)
{
if(!strcmp(W[i].origin,W[i-1].origin))
printf(" %s %s\n",W[i].name,W[i].level+1);
else
{
printf("%s:\n",W[i].origin);
printf(" %s %s\n",W[i].name,W[i].level+1);
}
}
}
}
return 0;
}
小白是用hash的思想做的 即 hash['w']=3, hash['g']=2, hash['s']=1; 比较好的思想哦。。。。。