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

#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整除。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值