【if语句实现】
#include <stdio.h>
#include <stdlib.h>
/*
Common year:365 -->31,28,31,30,31,30,31,31,30,31,30,31
Leap year:366 -->31,29,31,30,31,30,31,31,30,31,30,31
*/
int year,month,day;
int main() {
int flag=0, cnt=0;
int i;
scanf("%d-%d-%d", &year, &month, &day);
if(year%4==0 && year%100!=0 || year%400==0)
flag=1;
for(i=1; i<month; i++){
if(i==2 && flag==1)
cnt+=29;
else if(i==2)
cnt+=28;
else if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
cnt+=31;
else
cnt+=30;
}
cnt+=day;
printf("%d\n", cnt);
return 0;
}
/*
in:2019-3-1
out:60
*/
【switch语句实现】
#include <stdio.h>
#include <stdlib.h>
/*
Common year:365 -->31,28,31,30,31,30,31,31,30,31,30,31
Leap year:366 -->31,29,31,30,31,30,31,31,30,31,30,31
*/
int year,month,day;
int main() {
int flag=0, cnt=0;
int i;
scanf("%d-%d-%d", &year, &month, &day);
if(year%4==0 && year%100!=0 || year%400==0)
flag=1;
for(i=1; i<month; i++){
if(i==2 && flag==1)
cnt+=29;
switch(i){
case 2:cnt+=28;break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:cnt+=31;break;
default:cnt+=30;break;
}
}
cnt+=day;
printf("%d\n", cnt);
return 0;
}
/*
in:2019-3-1
out:60
*/