HDU3293 sort 【排序】

sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 694    Accepted Submission(s): 324

Problem Description
As is known to all, long long ago sailormoon once was an association of fighters. Till now, sailormoon is also an association of girls. Owe to some unknown reasons, girls are necessary to fight for peace.
Their boss, lcy, wants to strengthen their ability, so he give them his precious collections---weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so).
In order to make it clear, girls want to sort like this:
firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together;
secondly, sort according ranks, wonderful is the best, good is next, the third is so-so;
thirdly, if two or more have same origin and rank, sort them according to the lexicographic order.



Input
Input contains multiply cases. Each case contains several lines. First line is an integer N(0<N<=500), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness.
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
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct In
{
	char name[25];
	char origin[25];
	char level[25];
	int rank;
}weapon[510];
int cmp(In a,In b)
{
	if(strcmp(a.origin,b.origin)!=0)
	{
		return strcmp(a.origin,b.origin)<0;
	}
	else if(a.rank!=b.rank)
	{
		return a.rank<b.rank;
	}
	else
	{
		return strcmp(a.name,b.name)<0;
	}
}
int main()
{
	int n;
	int i,k=1;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			scanf("%s%s%s",weapon[i].name,weapon[i].origin,weapon[i].level);
			switch(weapon[i].level[0])
			{
				case 'w':weapon[i].rank=1;break;
				case 'g':weapon[i].rank=2;break;
				case 's':weapon[i].rank=3;break;
			}
		}
		sort(weapon,weapon+n,cmp);
		printf("Case %d\n",k++);
		for(i=0;i<n;i++)
		{
			if(i==0||strcmp(weapon[i].origin,weapon[i-1].origin)!=0)
			{
				printf("%s:\n",weapon[i].origin);
			}
			printf("%*s",10,"");
			printf("%s %s\n",weapon[i].name,weapon[i].level);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值