藍橋杯:歷年試題PREV-39— 日期問題
阿新 • • 發佈:2018-12-18
得到輸入,列出三種日期可能,判斷每種可能是否是正常日期;
對日期進行排序;
輸出不重複的正常日期。
#include<stdio.h> #include<stdlib.h> typedef struct { int year,month,day,none; }date; date sp[4]={0}; int getday(int year) { if((year%4==0&&year%100)||year%400==0) return 29; return 28; } void format(int year,int month,int day,int index) { sp[index].none=0; if(year>=60) year+=1900; else year+=2000; sp[index].year=year; if(month>12||month<1) { sp[index].none=1; return; } sp[index].month=month; int daylist[13]={0,31,getday(year),31,30,31,30,31,31,30,31,30,31}; if(day<1||day>daylist[month]) { sp[index].none=1; return; } sp[index].day=day; return; } int compare(const void *a,const void *b) { const date *sa=(const date*)a; const date *sb=(const date*)b; if(sa->year!=sb->year) return sa->year-sb->year; if(sa->month!=sb->month) return sa->month-sb->month; if(sa->day!=sb->day) return sa->day-sb->day; return 0; } int main(int argc,char **argv) { int aa,bb,cc; scanf("%d/%d/%d",&aa,&bb,&cc); format(aa,bb,cc,1); format(cc,aa,bb,2); format(cc,bb,aa,3); qsort(sp+1,3,sizeof(date),compare); int i; date history={0}; for(i=1;i<=3;i++) if(!sp[i].none&&compare(&history,&sp[i])) { history=sp[i]; printf("%d-%02d-%02d\n",sp[i].year,sp[i].month,sp[i].day); } return EXIT_SUCCESS; }