题目:
根据历法,凡是1、3、5、7、8、10、12月,每月31天;凡4、6、9、11月,每月30天;2月闰年29天,平年28天。 闰年判断方法是: 如果年号能被400整除,此年为闰年 如果年号能被4整除,而不能被100整除,此年为闰年; 否则不是闰年。 程序输入年、月,输出该月的天数。如果月份不为1~12之间的数字则转换为正确的月份,如13对应1月,不考虑月份为负数的情况。 例如: 输入:1992 3 输出:days:31 输入:2001 14 输出:days:28 |
分析:首先使用取模判断是否为闰年,且使用取模将月份转换为正确的月份,再将输入与输出一一对应,注意不要忽略输出中的days:;
#include <stdio.h>
int main()
{
int months, year, x; // x为1则为闰年,0为非闰年
scanf("%d %d", &year, &months);
if(months>12){
months = months % 12;}
if (months == 0) {
months = 12;
}
if (year % 400 == 0) {
x = 1;
} else if (year % 4 == 0 && year % 100 != 0) {
x = 1;
} else {
x = 0;
}
int ch1[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
ch0[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ch[12];
if (x) {
printf("days:%d", ch1[months - 1]);
} else {
printf("days:%d", ch0[months - 1]);
}
return 0;
}