HDU--杭电--3293--sort--结构体排序
2013-07-30 23:53
453 查看
sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 596 Accepted Submission(s): 274
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
[align=left]Sample Input[/align]
5
knife qizhou so-so
gun qizhou wonderful
knife zhengzhou good
stick zhengzhou good
rope shengzhou so-so
[align=left]Sample Output[/align]
Case 1
qizhou:
gun wonderful
knife so-so
shengzhou:
rope so-so
zhengzhou:
knife good
stick good
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct ssss
{
char mingzi[22],chandi[22],pinzhi[22];
//名字,产地,品质
}ss[555];
int good(const char *a,const
char *b) //做一个类似于strcmp的比较函数,按品质分别返回-1,0,1
{
if(!strcmp(a,"wonderful"))
if(!strcmp(b,"wonderful"))return
0;
else return 1;
if(!strcmp(a,"good"))
if(!strcmp(b,"wonderful"))return
-1;
else if(!strcmp(b,"good"))return
0;
else return 1;
if(!strcmp(b,"so-so"))return
0;
return -1;
}
bool cmp(const ssss &a,const
ssss &b)
{
if(strcmp(a.chandi,b.chandi)<0)return
1;
if(!strcmp(a.chandi,b.chandi))
if(good(a.pinzhi,b.pinzhi)>0||good(a.pinzhi,b.pinzhi)==0&&strcmp(a.mingzi,b.mingzi)<0)
return 1;
return 0;
}
int main (void)
{
int n,i,j,k,l=1;
char s[22];
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>ss[i].mingzi>>ss[i].chandi>>ss[i].pinzhi;
sort(ss,ss+n,cmp);
cout<<"Case "<<l++<<endl;
for(i=0;i<n;i++)
{
if(strcmp(s,ss[i].chandi))strcpy(s,ss[i].chandi),cout<<s<<":"<<endl;
//如果产地和s记录的不同,s换产地并输出产地
cout<<" "<<ss[i].mingzi<<"
"<<ss[i].pinzhi<<endl;
}
}
return 0;
}
相关文章推荐
- HDU 3293 sort(结构体排序)
- HDU--杭电--1177--"Accepted today?"--结构体排序
- hdu 3466 Proud Merchants (01背包 + 结构体的sort排序)
- HDU--杭电--2093--考试排名--结构体排序
- 【hdu 1862】EXCEL排序 (sort &结构体)
- HDU--杭电--1263--水果--结构体排序
- hdu 1263 水果 sort对结构体中字符串二级排序
- 杭电OJ(HDOJ)1862题:EXCEL排序(结构体sort)
- sort 1425 hdu(hash 和 sort排序)
- HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
- sort(HDU-3293)
- STL中的sort对结构体排序
- 初试结构体杭电1862EXCEL排序
- C# 对 结构体 或 类对象 的arraylist 排序 sort()
- HDU 3293 sort
- C++ sort()函数对结构体排序(STL例子)
- c++中sort的vector中结构体排序的两种方法说明
- <algorithm.h>中sort对结构体排序的用法
- 习题之用sort给结构体排序
- 终于学会了用sort排序结构体了。