#include <stdio.h> void day(int year, int month) { int arr[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //闰年月份天数 int brr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //平年月份天数 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) printf("%d\n", arr[month - 1]); else printf("%d\n", brr[month - 1]); } int main() { int a, b; while (scanf("%d %d", &a, &b) != EOF) { // 注意 while 处理多个 case day(a, b); } return 0; }
首先要清楚月份天数的分类
每年的4,6,9,11月的天数都是30天
每年的1,3,5,7,8,10,12月都是31天
然后计算2月的天数
2月的天数有两种,闰年为29天,非闰年28天
判断是否为闰年的标准:
(四年一闰&&百年不闰)||四百年一闰