PTA
1 #include<stdio.h> 2 int main() { 3 int year,month,day,cnt,flag; 4 flag = 0; 5 scanf("%4d/%2d/%2d",&year,&month,&day); 6 if((year%4 ==0 && year%100 != 0) || (year%400 == 0)) 7 flag = 1; 8 if(month == 1) 9 cnt = day; 10 if(month == 2) 11 cnt = 31 + day; 12 if(month == 3) 13 cnt = 31 + 28 + day + flag; 14 if(month == 4) 15 cnt = 31 + 28 + 31 + day + flag; 16 if(month == 5) 17 cnt = 31 + 28 + 31 + 30 + day + flag; 18 if(month == 6) 19 cnt = 31 + 28 + 31 + 30 + 31 + day + flag; 20 if(month == 7) 21 cnt = 31 + 28 + 31 + 30 + 31 + 30 + day + flag; 22 if(month == 8) 23 cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day + flag; 24 if(month == 9) 25 cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day + flag; 26 if(month == 10) 27 cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day + flag; 28 if(month == 11) 29 cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day + flag; 30 if(month == 12) 31 cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day + flag; 32 printf("%d",cnt); 33 }
简便的写法,用了数组和循环:
1 #include<stdio.h> 2 int main() { 3 int year,month,day,cnt,flag,i; 4 int a[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 5 flag = 0; 6 scanf("%4d/%2d/%2d",&year,&month,&day); 7 if((year%4 ==0 && year%100 != 0) || (year%400 == 0)) 8 flag = 1; 9 for(i = 1; i < month; i++) 10 cnt = cnt + a[i]; 11 cnt = cnt + day; 12 if(month > 2 && flag ==1) 13 cnt++; 14 printf("%d",cnt); 15 }