给出一个字符串代表日期,问它是这一年的第几天 | 我和闰年有个约会
#include <stdio.h>
int monthday[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int isR(int y){
return y%4==0&&y%100!=0||y%400==0;
}
int sumday[13];
int y,m,d;
char str[100];
int sum;
int main(){
scanf("%s",str);
sscanf(str,"%4d/%2d/%2d",&y,&m,&d);
if(isR(y))
{
monthday[2]=29;
}
sumday[0]=0;
for(int i=1;i<=m;i++){
sumday[i]=sumday[i-1]+monthday[i-1];
}
sum=sumday[m]+d;
printf("%d",sum);
}
相关习题:
char *week[]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int isR(int y){
return (y%4==0&&y%100!=0)||(y%400==0);
}
int monthday[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sumday=0;
char * dayOfTheWeek(int day, int month, int year){
for(int i=1971;i<year;i++){
if(isR(i)){
sumday+=366;
}
else
sumday+=365;
}
if(isR(year)){
.
monthday[2]=29;
}
for(int i=1;i<month;i++){
sumday+=monthday[i];
}
sumday+=day;
return week[(4+sumday)%7];
}