题解 | #获得月份天数#

获得月份天数

https://www.nowcoder.com/practice/13aeae34f8ed4697960f7cfc80f9f7f6

#include <stdio.h>

/*
判断是否为闰年
参数表:year年份
返回值:1是 0否
*/
int IsLeapYear(const int year)
{
	if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
		return 1;
	else
		return 0;
}

/*
计算任意年份月份的天数
参数表:year年份 month月份
返回值:year年month月的天数, 0不存在该年份或月份
*/
int CountDay(const int year, const int month)
{
	//判断年份或者月份是否合法
	if (year < 1 || month < 1 || month>12)
	{
		return 0;
	}

	//1,3,5,7,8,10,12月天数为31天
	if ((month < 8 && month % 2 == 1) || (month > 7 && month % 2 == 0))
	{
		return 31;
	}
	else if (2 == month)
	{//闰年2月有29天,平年闰月28天
		if (IsLeapYear(year))
			return 29;
		else
			return 28;
	}
	else
	{//剩余年份4,6,9,11月天数为30天
		return 30;
	}
	
}


int main() {
    int year = 0;
    int month = 0;
    while(scanf("%d %d", &year, &month) == 2)
    {
        printf("%d\n", CountDay(year, month));
    }
    return 0;
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-29 12:10
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务