题解 | #获得月份天数#
获得月份天数
https://www.nowcoder.com/practice/13aeae34f8ed4697960f7cfc80f9f7f6
#include <stdio.h>
int checkLunarYear(int year) {
int flagLuarYear = 0;
//闰年的条件是符合下面两个条件之一:
// 1、能被4整除,但不能被100整除;
// 2、能被4整除,又能被400整除)
if (((year % 4 == 0) && (year % 100 != 0)) || ((year % 4 == 0) &&
(year % 400 == 0))) {
flagLuarYear = 1;
return flagLuarYear;
}
return flagLuarYear;
}
int main() {
// 思路:
// 1, 是不是闰年? 闰年2月有29天,非闰年2月28天
// 2, 大月还是小月? 大月31天,小月30天
int year, month;
// 默认为闰年,注意设2月有29天
int arrMonth[12] = {
31, 29, 31, // 第一季度
30, 31, 30, // 第二季度
31, 31, 30, // 第三季度
31, 30, 31 // 第四季度
};
while (scanf("%d %d", &year, &month) != EOF) {
getchar();
// 是否是闰年
if (checkLunarYear(year)) {
// 通过数组下标索引到对应月份有几天
printf("%d\n", arrMonth[month - 1]);
} else {
// 条件表达式确认输入月份是否为2,是则减1,在printf()直接输入
// ATTENTION:不要给2月赋新值再输出,因为有下次循环继续判断,不需要改变月份的值
month == 2 ?
printf("%d\n", arrMonth[month - 1] - 1):
printf("%d\n", arrMonth[month - 1]);
}
}
return 0;
}
