7-19 计算天数
本题要求编写程序计算某年某月某日是该年中的第几天。
输入格式:
输入在一行中按照格式“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(){
int year,mouth,day;
scanf("%d/%d/%d",&year,&mouth,&day);
//printf("%d %d %d",year,mouth,day);
if(year%4==0){
if(year%400==0){
year=1;
//printf("year为闰年\n");
}else if(year%100!=0){
year=1;
//printf("year为闰年\n");
}
}else {
year=0;
//printf("year为平年");
}
if(year==1){
if(mouth>=3){
switch(mouth-1){
case 2:mouth=60;break;
case 3:mouth=91;break;
case 4:mouth=121;break;
case 5:mouth=152;break;
case 6:mouth=182;break;
case 7:mouth=213;break;
case 8:mouth=244;break;
case 9:mouth=274;break;
case 10:mouth=305;break;
case 11:mouth=335;break;
}
printf("%d",mouth+day);
}else{
switch(mouth-1){
case 0:mouth=0;break;
case 1:mouth=31;break;
}
printf("%d",mouth+day);
}
}else{
if(mouth>=3){
switch(mouth-1){
case 2:mouth=59;break;
case 3:mouth=90;break;
case 4:mouth=120;break;
case 5:mouth=151;break;
case 6:mouth=181;break;
case 7:mouth=212;break;
case 8:mouth=243;break;
case 9:mouth=273;break;
case 10:mouth=304;break;
case 11:mouth=334;break;
}
printf("%d",mouth+day);
}else{
switch(mouth-1){
case 0:mouth=0;break;
case 1:mouth=31;break;
}
printf("%d",mouth+day);
}
}
return 0;
}第 I 段——变量、表达式、分支、循环 文章被收录于专栏
中国大学MOOC 2021年春季C、Java入门练习第I段——变量、表达式、分支、循环

查看11道真题和解析