PAT Level B 1028 Census 20 (points)

topic

A census was conducted in a certain town and the birthdays of all residents were obtained. Now please write a program to find the oldest and youngest person in town.

Here to ensure that each date entered is legal, but not necessarily reasonable-assuming that there are no elderly people over 200 years old in the town, and today is September 6, 2014, so birthdays and dates over 200 years old are Birthdays are unreasonable and should be filtered out.

Input format:

In the first line of the input is given positive integer NN N , the value of (0, 105] (0, 10 ^ 5] ( 0 , 1 0 ? 5 ? ? ] ; Then NN N rows, each given 1 The name of the individual (a string of no more than 5 English letters), and the birthday given in the format (ie year/month/day). The title ensures that the oldest and youngest people are not tied together. yyyy/mm/dd

Output format:

Output the number of valid birthdays, the names of the oldest person and the youngest person in a row, separated by spaces.

Input sample:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

Sample output:

3 Tom John

Code


#include<iostream>
#include<algorithm>
using namespace std;
int compare(int* a, int* b)
{
    
    
	if (a[1] > b[1])
		return 1;
	else if (a[1] == b[1] && a[2] > b[2])
		return 1;
	else if (a[1] == b[1] && a[2] == b[2] && a[3] >= b[3])
		return 1;
	return 0;
}
int main()
{
    
    
	int num,i,n=0;
	cin >> num;
	string* names = new string[num],input;
	int** a = new int*[num],year,month,day;
	for (i = 0; i < num; i++)
		a[i] = new int[4];
	for(i=0;i<num;i++)
	{
    
    
		cin >> names[i] >> input;
		year = atoi((input.substr(0, 4)).c_str());
		month = atoi((input.substr(5, 2)).c_str());
		day= atoi((input.substr(8, 2)).c_str());
		if (((year<1814)||(year== 1814 &&month<9)|| (year == 1814 && month == 9&&day<6))|| ((year >2014) || (year == 2014 && month >9) || (year == 2014 && month == 9 && day > 6)))
		{
    
    
			n++;
			num--;
			i--;
		}
		else
		{
    
    
			a[i][0] = i;
			a[i][1] = year;
			a[i][2] = month;
			a[i][3] = day;
		}
	}
	sort(a,a+num,compare);
	if (num > 0)
		cout << num << " " << names[a[num - 1][0]] << " " << names[a[0][0]];
	else
		cout << 0;
	return 0;
}

Question details link

Guess you like

Origin blog.csdn.net/qq_41985293/article/details/114983427