输入某年某月某日,判断这一天是这一年的第几天? #include <stdio.h> int main(){ int year,month,day; int sum; int leap; int T; scanf("%d",&T); while(T--) { scanf("%d %d %d", 1 分 ); switch( 1 分 ) { case 1: sum=0; break; case 2: sum=31; break; case 3: sum=59; break; case 4: sum=89; break; case 5: sum=120; break; case 6: sum=151; break; case 7: sum=181; break; case 8: sum=212; break; case 9: sum=243; break; case 10: sum=273; break; case 11: sum=304; break; case 12: sum=334; break; 1 分 : printf("data error"); break; } sum+= day; if(year%400==0|| (year%4==0&&year%100!=0)) leap=1; else leap=0; if((leap == 1)&&(month>2&&month<=12)) sum++; printf("It is the %dth day.",sum); } return 0; }
时间: 2024-01-12 17:03:07 浏览: 92
C语言函数库全面详解
以下是完整程序:
```
#include <stdio.h>
int main() {
int year, month, day;
int sum;
int leap;
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d %d", &year, &month, &day);
if (month < 1 || month > 12 || day < 1) {
printf("data error");
continue;
}
switch (month) {
case 1:
sum = 0;
break;
case 2:
sum = 31;
break;
case 3:
sum = 59;
break;
case 4:
sum = 90;
break;
case 5:
sum = 120;
break;
case 6:
sum = 151;
break;
case 7:
sum = 181;
break;
case 8:
sum = 212;
break;
case 9:
sum = 243;
break;
case 10:
sum = 273;
break;
case 11:
sum = 304;
break;
case 12:
sum = 334;
break;
default:
printf("data error");
continue;
}
sum += day;
if ((year%4 == 0 && year%100 != 0) || year%400 == 0) {
leap = 1;
} else {
leap = 0;
}
if (leap == 1 && month > 2) {
sum++;
}
printf("It is the %dth day.\n", sum);
}
return 0;
}
```
程序首先读入测试数据的组数 T。接着进入循环,每次循环读入一组测试数据,即年月日。如果月份不在 1~12 范围内,或者日期不在 1~31 范围内,程序会输出 "data error" 并跳过这组数据,继续下一次循环。
如果输入的月份和日期都在合法范围内,程序会根据输入的月份计算当月之前的天数,并加上输入的日期,得到当年的第几天。如果是闰年,并且输入的月份在 3~12 之间,还需要额外加上一天。
最后程序输出结果,即当年的第几天。
阅读全文