本题要求编写程序计算某年某月某日是该年中的第几天。
输入格式:
输入在一行中按照格式“yyyy/mm/dd”(即“年/月/日”)给出日期。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。闰年的2月有29天。
输出格式:
在一行输出日期是该年中的第几天。
输入样例1:
2009/03/02
输出样例1:
61
输入样例2:
2000/03/02
输出样例2:
62
第一种方法:
#include<stdio.h>
int main(void)
{
int year,month,day;
scanf("%d/%d/%d",&year,&month,&day);
//printf("%d:%d:%d",year,month,day);
if((year%40&&year%100!=0)||(year%4000)){
switch(month){
case 1:printf("%d",day);break;
case 2:printf("%d",31+day);break;
case 3:printf("%d",31+29+day);break;
case 4:printf("%d",312+29+day);break;
case 5:printf("%d",312+29+30+day);break;
case 6:printf("%d",313+29+30+day);break;
case 7:printf("%d",313+29+302+day);break;
case 8:printf("%d",314+29+302+day);break;
case 9:printf("%d",315+29+302+day);break;
case 10:printf("%d",315+29+303+day);break;
case 11:printf("%d",316+29+303+day);break;
case 12:printf("%d",316+29+304+day);
}
}else{
switch(month){
case 1:printf("%d",day);break;
case 2:printf("%d",31+day);break;
case 3:printf("%d",31+28+day);break;
case 4:printf("%d",312+28+day);break;
case 5:printf("%d",312+28+30+day);break;
case 6:printf("%d",313+28+30+day);break;
case 7:printf("%d",313+28+302+day);break;
case 8:printf("%d",314+28+302+day);break;
case 9:printf("%d",315+28+302+day);break;
case 10:printf("%d",315+28+303+day);break;
case 11:printf("%d",316+28+303+day);break;
case 12:printf("%d",316+28+304+day);
}
}
return 0;
}
第二种方法:(通过自己编写函数)
#include<stdio.h>
void Sumday(int flag,int month,int day){
int sum=0;
int Str[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};
if(flag1){
for(int i=0;i<month-1;i++){
sum+=Str[0][i];
}
printf("%d",sum+day);
}else{
for(int i=0;i<month-1;i++){
sum+=Str[1][i];
}
printf("%d",sum+day);
}
}
int main(void)
{
int year,month,day,flag;
scanf("%d/%d/%d",&year,&month,&day);
if((year%40&&year%100!=0)||(year%400==0)){
flag=1;
}else{
flag=0;
}
Sumday(flag,month,day);
return 0;
}