学习笔记:
作业:
1.
#include <stdio.h>
// 函数声明,用于判断闰年
int isLeapYear(int year);
int main() {
int y, m, d;
scanf("%d %d %d", &y, &m, &d);
if (m < 1 || m > 12 || d < 1 || d > 31) {
printf("输入的日期无效。\n");
return 1;
}
int x = 0;
switch (m - 1) {
case 0:
x += 0;
break;
case 1:
x += isLeapYear(y) ? 29 : 28;
break;
case 2:
x += 31;
break;
case 3:
x += 31 + 28;
break;
case 4:
x += 31 + 28 + 31;
break;
case 5:
x += 31 + 28 + 31 + 30;
break;
case 6:
x += 31 + 28 + 31 + 30 + 31;
break;
case 7:
x += 31 + 28 + 31 + 30 + 31 + 31;
break;
case 8:
x += 31 + 28 + 31 + 30 + 31 + 31 + 30;
break;
case 9:
x += 31 + 28 + 31 + 30 + 31 + 31 + 30 + 31;
break;
case 10:
x += 31 + 28 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
case 11:
x += 31 + 28 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;
break;
}
x += d;
printf("输入的日期是一年中的第%d天。\n", x);
return 0;
}
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}