输出闰年的代码

这个题教会我要提前把每种情况都考虑到

后续直接使用就方便了

比如我的代码 先提前列好每个月份的情况 再去直接循环判断调用

#include <stdio.h>

int isLeapYear(int year) {

return (year % 4 == 0 && year % 100!= 0) || year % 400 == 0;

}

int daysInMonth(int year, int month) {

int days;

switch (month) {

case 2:

days = isLeapYear(year)? 29 : 28;

break;

case 4: case 6: case 9: case 11:

days = 30;

break;

default:

days = 31;

}

return days;

}

int main() {

int year, month;

scanf("%d %d", &year, &month);

int days = daysInMonth(year, month);

printf("%d\n", days);

return 0;

}

全部评论

相关推荐

12-19 22:04
武汉大学 Java
点赞 评论 收藏
分享
12-15 14:16
门头沟学院 Java
回家当保安:发offer的时候会背调学信网,最好不要这样。 “27届 ”和“28届以下 ”公司招聘的预期是不一样的。
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务