qfedu-c-level/day4/homework/h14.c

45 lines
1.1 KiB
C
Raw Permalink Normal View History

2023-07-08 16:27:14 +08:00
// 【扩展题2】请编程,从键盘接收年、月、日三个整数,判断是这一年的第几天。
// 【提示】必然考虑年是否为闰年,闰年条件:能被4整除且不能被100整除,或者能被400整除
#include <stdio.h>
int isRun(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 1; // 是闰年
return 0;
}
int main()
{
int year, month, day;
year = scanf("%d", &year);
month = scanf("%d", &month);
day = scanf("%d", &day);
int days = 0;
int mon_2 = isRun(year) ? 29 : 28; // 二月天数: 闰年29天, 平年28天
if (month < 8)
{
if (month % 2 == 0)
{
days += month / 2 * 31 + (month / 2 - 1) * 30 + mon_2;
printf("%d\n", days);
}
else
{
days += (month / 2 + 1) * 31 + (month / 2 - 1) * 30 + mon_2;
printf("%d\n", days);
}
}
else
{
if (month % 2 == 0)
{
days += (month / 2 - 1) * 31 + month / 2 * 30 + mon_2;
}
else
{
days += (month / 2) * 31 + (month / 2) * 30 + mon_2;
}
}
}