c——闰年

时间:2023-12-17 08:08:44

PTA

 #include<stdio.h>
int main() {
int year,month,day,cnt,flag;
flag = ;
scanf("%4d/%2d/%2d",&year,&month,&day);
if((year% == && year% != ) || (year% == ))
flag = ;
if(month == )
cnt = day;
if(month == )
cnt = + day;
if(month == )
cnt = + + day + flag;
if(month == )
cnt = + + + day + flag;
if(month == )
cnt = + + + + day + flag;
if(month == )
cnt = + + + + + day + flag;
if(month == )
cnt = + + + + + + day + flag;
if(month == )
cnt = + + + + + + + day + flag;
if(month == )
cnt = + + + + + + + + day + flag;
if(month == )
cnt = + + + + + + + + + day + flag;
if(month == )
cnt = + + + + + + + + + + day + flag;
if(month == )
cnt = + + + + + + + + + + + day + flag;
printf("%d",cnt);
}

简便的写法,用了数组和循环:

 #include<stdio.h>
int main() {
int year,month,day,cnt,flag,i;
int a[] = {,,,,,,,,,,,};
flag = ;
scanf("%4d/%2d/%2d",&year,&month,&day);
if((year% == && year% != ) || (year% == ))
flag = ;
for(i = ; i < month; i++)
cnt = cnt + a[i];
cnt = cnt + day;
if(month > && flag ==)
cnt++;
printf("%d",cnt);
}