1028. Census (20)
A town conducts a census and gets the birthdays of all its residents. Now please write a program to find the oldest and youngest people in town.
This ensures that each date entered is legal, but not necessarily reasonable - assuming it is known that there are no old people over 200 in town, and today is September 6, 2014, so birthdays over 200 and not Birth birthdays are all unreasonable and should be filtered out.
Input format:
The input gives a positive integer N in the first line, with a value in (0, 10 5 ]; next N lines, each line gives a person's name (a string consisting of no more than 5 English letters), and press " Birthday given in yyyy/mm/dd" (ie year/month/day) format. The title ensures that the oldest and youngest people are not tied.
Output format:
Print the number of valid birthdays, the names of the oldest and youngest, in order on a single line, 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/20Sample output:
3 Tom John
#include<cstdio> #include<algorithm> using namespace std; struct node{ char name[10]; int y, m, d; }arr[100005]; int main(){ //freopen("input.txt", "r", stdin); int n, len = 0, g1, g2; int maxage = 20140906, young = 0; int minage = 18140906, old = 99999999; scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%s %d/%d/%d", arr[len].name, &arr[len].y, &arr[len].m, &arr[len].d); int x = arr[len].y * 10000 + arr[len].m *100 + arr[len].d; // Convert the number to yyyymmdd and compare the size directly if(x > maxage) continue; if(x < minage) continue; if(x > young) young = x, g1 = len; if(x < old) old = x, g2 = len; len ++; } if(len != 0)printf("%d %s %s", len, arr[g2].name, arr[g1].name); else printf("0"); return 0; }