题解 | 获得月份天数
获得月份天数
https://www.nowcoder.com/practice/13aeae34f8ed4697960f7cfc80f9f7f6
#include <stdio.h> short leapyear(int year)//判断是否为闰年。f=1是闰年。f=0,不是闰年 { short f = 0; if (year % 400 == 0) f = 1; if (year % 4 == 0 && year % 100 != 0) f = 1; return f; } int month_days(int year, int month)//返回year,month的天数 { int d[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (leapyear(year)) d[2] = 29; return d[month]; } int main(int argc, char const* argv[]) { int month = 0, year = 0; while ((scanf("%d%d", &year, &month)) == 2) { if (month > 12 || month < 1) { printf("月份输入有误,请重新输入\n"); continue; } printf("%d\n", month_days(year, month)); } }