三天打鱼两天晒网
1 #include<stdio.h> 2 3 4 /** 5 *判断闰年 6 */ 7 int isR(int year){ 8 if((year%4==0 || year%100==0)&&year%400!=0) { 9 return 1; 10 } 11 return 0; 12 } 13 14 /** 15 *如果参数合法,返回其至1990-01-01的时间长度 16 *否则返回0 17 */ 18 int isLegal(int year, int m, int day) { 19 //对月份进行初始化 20 int isr = isR(year); 21 int month[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31}; 22 if(isr) { 23 month[2]=28; 24 } 25 26 //逐一校验 27 if(year < 1990) { 28 return 0; 29 } 30 if(m<=0 || m>12) { 31 return 0; 32 } 33 //日期合法性需要有月份来决定 34 switch(m) { 35 case 1: case 5: case 3: case 7: case 8: case 10: case 12: 36 if(day < 0 || day > 31){ 37 return 0; 38 } 39 case 2: 40 if(isr == 1){ 41 if(day > 28 || day < 0){ 42 return 0; 43 } 44 }else { 45 if(day > 29 || day < 0){ 46 return 0; 47 } 48 } 49 default: 50 if(day <0 || day > 30){ 51 return 0; 52 } 53 } 54 55 int L=0; 56 //遍历年 57 for(int i=1990; i<year; i++){ 58 if(isR(i)){ 59 L+=366; 60 }else{ 61 L+=365; 62 } 63 } 64 //遍历月 65 for(int i=1; i<m; i++){ 66 L+=month[i]; 67 } 68 //遍历日 69 L+=day; 70 return L; 71 } 72 73 74 int main() { 75 int year, month, day; 76 scanf("%4d-%2d-%2d", &year,&month,&day); 77 int L=isLegal(year, month, day); 78 if(L==0){ 79 puts("Invalid input"); 80 }else{ 81 int n=L%5; 82 if(n>0 && n<=3){ 83 puts("He is working"); 84 }else{ 85 puts("He is having a rest"); 86 } 87 } 88 return 0; 89 }