您的位置:首页 > 其它

生日相同 2.0

2015-10-23 00:13 295 查看
总时间限制: 
1000ms
 
内存限制: 
65536kB
描述

在一个有180人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的名字,出生月日。试找出所有生日相同的学生。

输入
第一行为整数n,表示有n个学生,n ≤ 180。此后每行包含一个字符串和两个整数,分别表示学生的名字(名字第一个字母大写,其余小写,不含空格,且长度小于20)和出生月(1 ≤ m ≤ 12)日(1 ≤ d ≤ 31)。名字、月、日之间用一个空格分隔
输出
每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的名字,数字、名字之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。 对生日相同的名字,按名字从短到长按序输出,长度相同的按字典序输出。如没有生日相同的学生,输出”None”
样例输入
6
Avril 3 2
Candy 4 5
Tim 3 2
Sufia 4 5
Lagrange 4 5
Bill 3 2
样例输出
3 2 Tim Bill Avril
4 5 Candy Sufia Lagrange

string 的STL大法+模拟
#include<bits/stdc++.h>
using namespace std;
struct node{
string s;
int yue,ri;
}a[200];
int N;
int cmp(const node&w,const node&e){
if(w.yue<e.yue) return 1;
else if(w.yue==e.yue){
if(w.ri<e.ri) return 1;
else if(w.ri==e.ri){
if(w.s.length()<e.s.length()) return 1;
else if(w.s.length()==e.s.length()){
if(w.s<e.s) return 1;
}
}
}
return 0;
}
int tot;
string ans[200];
bool jud=false;
int main(){
scanf("%d",&N);
for(int i=1;i<=N;i++){
cin>>a[i].s;
cin>>a[i].yue>>a[i].ri;
}
sort(a+1,a+N+1,cmp);
a[N+1].yue=100; a[N+1].ri=100;
for(int i=2;i<=N+1;i++){
if(a[i].yue==a[i-1].yue&&a[i].ri==a[i-1].ri){
tot++;
if(tot==1){
ans[tot]=a[i-1].s;
ans[++tot]=a[i].s;
}
else{
ans[tot]=a[i].s;
}
}
else{
if(tot>=2){
jud=true;
cout<<a[i-1].yue<<" "<<a[i-1].ri<<" ";
for(int i=1;i<=tot;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
tot=0;
}
}
}
if(jud==false){
cout<<"None";
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: