题目
思路
这道题我们可以直接模拟寻找过程。
先把能合并的生日和名字合并,
然后再把重复人数大于等于二的提取出来,
最后关键字排序输出。
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
int n,w,zsl;
struct node
{
string s;
int sy,sr;
}a[10010];
struct node2
{
string s2[100];
int sy2,sr2,cc;
}a2[10010];
struct node3
{
string s3[100];
int sy3,sr3,ccc;
}a3[10010];
bool cmp(const node&x,const node&y)
{
if(x.s.size()==y.s.size())
return x.s<y.s;
else
return x.s.size()<y.s.size();
}
bool cmp2(const node3&x,const node3&y)
{
if(x.sy3!=y.sy3)
return x.sy3<y.sy3;
if(x.sy3==y.sy3)
return x.sr3<y.sr3;
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i].s>>a[i].sy>>a[i].sr;
sort(a+1,a+1+n,cmp);
/*for(int i=1; i<=n; i++)
cout<<a[i].s<<" "<<a[i].sy<<" "<<a[i].sr<<endl;*/
for(int i=1; i<=n; i++) 合并相同生日🎂
{
w=0;
for(int j=1; j<=zsl; j++)
{
if(a[i].sy==a2[j].sy2&&a[i].sr==a2[j].sr2)
{
a2[j].cc++,a2[j].s2[a2[j].cc]=a[i].s,w=1;
break;
}
}
if(w==0)
{
zsl++;
a2[zsl].cc=1,a2[zsl].s2[1]=a[i].s;
a2[zsl].sy2=a[i].sy,a2[zsl].sr2=a[i].sr;
}
}
int c12=0;
// for(int i=1; i<=zsl; i++)
// cout<<a2[i].cc<<endl;
for(int i=1; i<=zsl; i++)
if(a2[i].cc>=2) 提取大于等于二的人数🌧
{
a3[++c12].sr3=a2[i].sr2,a3[c12].sy3=a2[i].sy2;
a3[c12].ccc=a2[i].cc;
for(int j=1; j<=a2[i].cc; j++)
a3[c12].s3[j]=a2[i].s2[j];
//cout<<a3[c12].sr3<<" "<<a3[c12].sy3<<" "<<a3[c12].ccc<<endl;
}
sort(a3+1,a3+1+c12,cmp2); 排序输出📖
for(int i=1; i<=c12; i++)
{
cout<<a3[i].sy3<<" "<<a3[i].sr3<<" ";
for(int j=1; j<=a3[i].ccc; j++)
cout<<a3[i].s3[j]<<" ";
cout<<endl;
}
return 0;
}