题目地址:https://www.nowcoder.com/pat/6/problem/4054
题解:结构体排序即可
1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : YCute 5 * Date : 2019-11-19-20.13.37 6 * Description : 结构体排序 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<cmath> 11 #include<cstring> 12 #include<algorithm> 13 using namespace std; 14 15 struct bir{ 16 char name[10]; 17 int year; 18 int month; 19 int day; 20 }; 21 22 int cmp(struct bir a,struct bir b){ 23 if(a.year<b.year) return 1; 24 else if(a.year==b.year){ 25 if(a.month<b.month) return 1; 26 else if(a.month==b.month){ 27 if(a.day<b.day) return 1; 28 else return 0; 29 }else return 0; 30 }else{ 31 return 0; 32 } 33 } 34 35 int main(){ 36 int n; 37 struct bir b[100005]; 38 scanf("%d",&n); 39 getchar(); 40 for(int i=0;i<n;i++){ 41 scanf("%s %4d/%2d/%2d",b[i].name,&b[i].year,&b[i].month,&b[i].day); 42 } 43 sort(b,b+n,cmp); 44 int i=0; 45 while(1){ 46 if(b[i].year<1814){ 47 i++; 48 continue; 49 } 50 else if(b[i].year==1814){ 51 if(b[i].month<9){ 52 i++; 53 continue; 54 } 55 else if(b[i].month==9){ 56 if(b[i].day<6){ 57 i++; 58 continue; 59 } 60 else break; 61 }else{ 62 break; 63 } 64 }else{ 65 break; 66 } 67 } 68 int j=n-1; 69 while(j){ 70 if(b[j].year>2014){ 71 j--; 72 continue; 73 } 74 else if(b[j].year==2014){ 75 if(b[j].month>9){ 76 j--; 77 continue; 78 }else if(b[j].month==9){ 79 if(b[j].day>6){ 80 j--; 81 continue; 82 } 83 else break; 84 }else break; 85 }else { 86 break; 87 } 88 89 } 90 printf("%d %s %s",j-i+1,b[i].name,b[j].name); 91 return 0; 92 }