"Ybtoj High-Efficiency Advanced" Part Two Chapter One Example Question 5 Same birthday

General idea

In a large class, there is a very high probability that there are two people with the same birthday. Now each student is given the name, month and day of birth. Try to find all students with the same birthday.
For all output, it is required to output in the order of date from front to back.
Names with the same birthday are output in order from short to long, and those with the same length are output in lexicographic order.

Ideas

Um, it is obviously structure + sorting, excessive water
code:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct f{
    
    
	int x,y;
	string z;
} a[100001];
bool o;
bool cmp(f a,f b)
{
    
    
	if (a.x==b.x)
	{
    
    
		if (a.y==b.y)
		{
    
    
			if (a.z.size()==b.z.size())
			{
    
    
				return a.z<b.z;
			}
			return a.z.size()<b.z.size();
		}
		return a.y<b.y;
	}
	return a.x<b.x;
}
int main()
{
    
    
	int n;
	cin>>n;
	for (int i=1;i<=n;i++) cin>>a[i].z>>a[i].x>>a[i].y;
	sort(a+1,a+n+1,cmp);
	for (int i=1;i<=n;i++)
	{
    
    
		if (a[i].x==a[i-1].x&&a[i].y==a[i-1].y) cout<<a[i].z<<' ';
		else
		{
    
    
			if (o) cout<<endl;
			o=1;
			cout<<a[i].x<<' '<<a[i].y<<' '<<a[i].z<<' ';
		}
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_49843717/article/details/113832777