PAT B-1028 census

A town census, was the birthday of all residents. Now you write a program to find the town's oldest and most young people.

Make sure that each date entered here are legitimate, but not necessarily reasonable - assuming there is no known town more than 200 years old, and today is September 6, 2014, the more than 200-year-old's birthday and not born birthday is unreasonable and should be filtered out.

Input formats:

In the first line of the input is given a positive integer  N, the value (; then  N rows, each individual is given a name (a string of not more than 5 letters of the English), and press  yyyy/mm/dd(i.e., year / month / day) birthday format given in the title to ensure the oldest and the youngest person not tied.

Output formats:

The number of valid order of the output birthday in a row, young people and most of the oldest names, separated by spaces between them.

Sample input:

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
achieve:
# include <cstdio>
struct person{
char name[10];
int yy,mm,dd;
}oldest,youngest,left,right;
bool more(person a,person b){
if(a.yy!=b.yy) return a.yy>=b.yy;
else if(a.mm!=b.mm) return a.mm>=b.mm;
else return a.dd>=b.dd;
}
bool less(person a,person b){
if(a.yy!=b.yy) return a.yy<=b.yy;
else if(a.mm!=b.mm) return a.mm<=b.mm;
else return a.dd<=b.dd;
}
int init () {// set the oldest, the youngest of the initial and boundary
oldest.yy=right.yy=2014;
youngest.yy=left.yy=1814;
oldest.mm=youngest.mm=left.mm=right.mm=9;
oldest.dd=youngest.dd=left.dd=right.dd=6;
}
int main () {
init();
int n, num = 0;
person temp;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s %d/%d/%d", &temp.name, &temp.yy, &temp.mm, &temp.dd);
if(more(temp,left)&&less(temp,right)){//日期合法
a ++;
if (more (temp, youngest)) youngest = temp; // The larger the value, the Young
// The smaller the value, the older; if (less (temp, oldest)) oldest = temp

}
}
// output
if (num==0) printf("0\n");
else{
printf("%d %s %s\n", num, oldest.name, youngest.name);
}
return 0;
}

Guess you like

Origin www.cnblogs.com/lzdxh027/p/11305577.html