HDU 3293:sort
2016-07-11 15:17
344 查看
[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 <algorithm>
#include <string.h>
using namespace std;
struct sa
{
char a[25];
char b[25];
int c;
}str[550];
char k[3][20]={"so-so","good","wonderful"};
int l=1;
int cmp(const sa &p,const sa &q)
{
if(strcmp(p.b,q.b)!=0)
return strcmp(p.b,q.b)<0;
else if(p.c!=q.c)
return p.c>q.c;
else
return strcmp(p.a,q.a)<0;
}
int main()
{
int n;
while(cin>>n)
{
char tmp[25];
for(int i=0;i<n;i++)
{
cin>>str[i].a>>str[i].b;
cin>>tmp;
if(strcmp(tmp,"so-so")==0)
str[i].c=0;
if(strcmp(tmp,"good")==0)
str[i].c=1;
if(strcmp(tmp,"wonderful")==0)
str[i].c=2;
}
sort(str,str+n,cmp);
//for(int i=0;i<n;i++)
//cout<<str[i].a<<" "<<str[i].b<<" "<<str[i].c<<endl;
cout<<"Case "<<l++<<endl;
cout<<str[0].b<<":"<<endl;
cout<<" "<<str[0].a<<" "<<k[str[0].c]<<endl;
for(int i=1;i<n;i++)
{
if(strcmp(str[i].b,str[i-1].b)==0)
cout<<" "<<str[i].a<<" "<<k[str[i].c]<<endl;
else
{
cout<<str[i].b<<":"<<endl;
cout<<" "<<str[i].a<<" "<<k[str[i].c]<<endl;
}
}
}return 0;
}
加油!
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 <algorithm>
#include <string.h>
using namespace std;
struct sa
{
char a[25];
char b[25];
int c;
}str[550];
char k[3][20]={"so-so","good","wonderful"};
int l=1;
int cmp(const sa &p,const sa &q)
{
if(strcmp(p.b,q.b)!=0)
return strcmp(p.b,q.b)<0;
else if(p.c!=q.c)
return p.c>q.c;
else
return strcmp(p.a,q.a)<0;
}
int main()
{
int n;
while(cin>>n)
{
char tmp[25];
for(int i=0;i<n;i++)
{
cin>>str[i].a>>str[i].b;
cin>>tmp;
if(strcmp(tmp,"so-so")==0)
str[i].c=0;
if(strcmp(tmp,"good")==0)
str[i].c=1;
if(strcmp(tmp,"wonderful")==0)
str[i].c=2;
}
sort(str,str+n,cmp);
//for(int i=0;i<n;i++)
//cout<<str[i].a<<" "<<str[i].b<<" "<<str[i].c<<endl;
cout<<"Case "<<l++<<endl;
cout<<str[0].b<<":"<<endl;
cout<<" "<<str[0].a<<" "<<k[str[0].c]<<endl;
for(int i=1;i<n;i++)
{
if(strcmp(str[i].b,str[i-1].b)==0)
cout<<" "<<str[i].a<<" "<<k[str[i].c]<<endl;
else
{
cout<<str[i].b<<":"<<endl;
cout<<" "<<str[i].a<<" "<<k[str[i].c]<<endl;
}
}
}return 0;
}
加油!
相关文章推荐
- Binary Tree Level Order Traversal
- 用平方根判断素数
- c#使用扫描仪
- 用swoole扩展编写WebSocket聊天室
- 分布式存储Ceph
- 台湾大学林轩田教授机器学习基石课程理解及python实现----PLA
- java实现Http协议
- Spring3+Hibernate4连接Oracle11g数据库参数配置
- HDU 1710 Binary Tree Traversals
- 跳转微信
- 指针p,q,r依次指向某循环链表中三个相邻的结点,交换结点*q,*r在表中次序的程序段是
- Spring3+Hibernate4连接Oracle11g数据库参数配置
- CodeForces 690D1 The Wall (easy) (判断连通块的数量)
- 水果拼盘
- java 自定义标签 传值
- 入门自定义标签,(在SSH里面有自定义标签的练习)
- HDU 1392 Surround the Trees
- Shell脚本中计算字符串长度的5种方法
- 321. Create Maximum Number
- 环境变量