编程实现统计某年某月份的天数

xiaoxiao2021-02-28  95

#include <stdio.h> int main() { int a,b; char c; printf("Please enter the month to query, format such as: 2017.7.\n"); scanf("%d%c%d", &a, &c, &b); switch (b) { case 2 : if(a % 4 != 0 ||(a % 4 ==0 && a % 400 != 0)) { printf("There are 28 days in this month.\n"); break; } else { printf("There are 29 days in this month.\n"); break; } case 4 : case 6 : case 9 : case 11 : printf("There are 30 days in this month.\n"); break; case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : printf("There are 31 days in this month.\n"); break; default : printf("Sorry, you input errors.\n"); break; } return 0; }

在学了switch之后,这种问题的实现很简单。

首先,一年有12个月,这十二个月又分为小月,大月还有一个特殊的二月。

我们先对这个特殊的二月进行讨论,二月两个情况,闰年有29天,非闰年有28天。

闰年的情况是年数可以被4整除不能被100整除,或者可以被400整除。

然后对小月进行判断,之后对大月进行判断。

转载请注明原文地址: https://www.6miu.com/read-62054.html

最新回复(0)