数据结构:生日相同2.0(c++版本)
6377:生日相同 2.0
题目:
在一个有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
我的解答思路:
首先建立结构体,然后建立一个x[13][32]的数组存放日期,然后用两个for循环保证输出的日期顺序符合要求,然后设定d,使得只有至少两个人生日相同时才会输出。废话不多说我们上代码:
#include<iostream> #include<string.h> #include<algorithm> using namespace std; int cmp(struct stu x,struct stu y) struct stu { char name[20]; int month; int day; int l; }stu[181]; int cmp(struct stu x,struct stu y) { x.l=strlen(x.name); y.l=strlen(y.name); if(x.month==y.month) { if(x.day==y.day) { if(x.l==y.l) { return strcmp(x.name,y.name)<0; } return x.l<y.l; } return x.day<y.day; } return x.month<y.month; } int main() { int x[13][32],n,r=0,d=0; cin>>n; memset(x,0,sizeof(x)); for(int i=1;i<n;i++) { cin>>stu[i].name>>stu.[i].month>>stu[i].day; x[stu[i].month][stu[i].day]++; } sort(stu,stu+n,cmp); //排序名字 for(int i=1;i<13;i++) { for(int j=1;j<32;j++) { if(x[i][j]>0) //保证输出的时间是按照顺序来的, { d=0; for(int k=0;k<0;k++) { if(stu[k].month==i&&stu[k].day==j) { d++; } } if(d>=2) //主要是为了确定有生日相同的人再进行输出 { r=1; //确认书出过 cout<<i<<" "<<j; for(int q=0;q<n;q++) { if(stu[q].month==i&&stu[q].day==j) { cout<<" "<<stu[q].name; } } cout<<endl; } } } } if(r==0) //没有生日相同的人 { cout<<"None"; } rerturn 0; }
上一篇:
微信小程序ssm电影院售票系统app
下一篇:
软考(软件设计师知识点) --多媒体基础