Same birthday (string processing)

Same birthday

Insert picture description here

Problem solving ideas

Sort them first, the
first date is small to large ( month first and day later is better)
if they are equal, the length of the name is short to long.
If equal, the lexicographic order of the name is small to large

Then judge if there is any duplication

AC code

#include<cstdio>
#include<iostream> 
#include<algorithm>
using namespace std;
int n,ok;
struct node
{
    
    
	string name;
	int month,day;
}a[100005];
bool cmp(node x,node y)//排序规则
{
    
    
	if(x.month!=y.month)return x.month<y.month;
	if(x.day!=y.day)return x.day<y.day;
	if(x.name.size()!=y.name.size())return x.name.size()<y.name.size();
	return x.name<y.name;
}
int main()
{
    
    
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	 cin>>a[i].name>>a[i].month>>a[i].day;
	sort(a+1,a+n+1,cmp);
	int i=1;
	while(i<=n)//判断是否有相等的
	{
    
    
		if(a[i].month==a[i+1].month&&a[i].day==a[i+1].day)
		{
    
    
			cout<<a[i].month<<' '<<a[i].day<<' '<<a[i].name;
			while(a[i].month==a[i+1].month&&a[i].day==a[i+1].day)
			{
    
    
				ok=1;
				i++;
				cout<<' '<<a[i].name;
			}
			printf("\n");
		}
		i++;
	}
	if(ok==0)printf("None");//输出
	return 0;
}

Thank you

Guess you like

Origin blog.csdn.net/weixin_45524309/article/details/113172196