PAT (Basic Level) Practice (Chinese) 1028 Census (20 points)

 

1028 Census (20 minutes)

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 values (0,10 5]; followed by N rows, each individual is given a name (a string of not more than 5 letters of the English), and by  yyyy/mm/ddbirth (ie year / month / day) 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

 

Ideas: Note special sentenced reasonable number is zero.

 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
	   char name[109];
	   int yy,mm,dd;
}a[100009];
int cmp( struct Node a ,struct Node b ){
	if ( a.yy != b.yy ){
		 return a.yy < b.yy;
	}
	if(  a.mm != b.mm ){
		 return a.mm <b.mm;
	}
	return a.dd < b.dd;
}
bool check( int yy ,int mm ,int dd ){
	 if(  yy< 1814    )
	      return false;
	 else if( yy == 1814 ){
	 	  if( mm < 9 ){
	 	 	 return false;
		  }
		  else if( mm == 9 ){
		 	   if( dd <6 ){
		 	  	   return false;
			   }
		 } 
	 }
	 
	 else  if(   yy > 2014  )
	       return false;
  	 else  if(   yy ==2014 ){
	 	 if( mm >9 ){
	 	 	 return false;
		  }
	 	 else if( mm == 9 ){
	 	 	  if( dd >6 ){
	 	 	  	  return false;
		   	  }
		  }
	 }
	 return true;
}
int main(void){
	
	int n;
	while(  scanf("%d",&n)!=EOF){
		    int ans = 0,yy,mm,dd;
		    char name[109];
			for( int i=0;i<n;i++){
				 scanf("%s",name);
				 scanf("%d",&yy);
				 getchar();
				 scanf("%d",&mm);
				 getchar();
				 scanf("%d",&dd);
			     if( check(yy,mm,dd) ){
				     strcpy(a[ans].name,name);
				     a[ans].yy = yy;
				     a[ans].mm = mm;
				     a[ans].dd = dd;
				     ans++;
			     }
			}
			sort(a,a+ans,cmp);
			if(  ans ==0 ){ 
	             printf("0\n");
			}
			else printf("%d %s %s",ans,a[0].name,a[ans-1].name);
	
	}
	
	
	return 0;
} 

 

Guess you like

Origin blog.csdn.net/S_999999/article/details/94733990