输出每月的天数--华为机试题

输入含年月的字符串,仅支持信息格式为MM-YYYY(MM:月份,YYYY:年份),根据该消息输出当月总天数。注意考虑闰年。

(1)公历年份是4的倍数且不是100的倍数。

(2)公历年份是400的倍数

例如,2000年是闰年,1900则是平年。闰年2月份为29天,平年2月份为28天。

运行时间限制:无限制

内存限制:无限制

输入:年月信息字符串

输出:当月总天数

样例输入:03-2012

样例输出:31

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string a;//用来接收年月的字符串
	int month,year;//存储转换好的年,月
	bool flag;//false表示平年,ture表示闰年
	cin>>a;
	if (7!=a.length())
	{
		cout<<"输入的字符串的有误,请重新输入."<<endl;
	}
	month=10*(a[0]-'0')+a[1]-'0';
	year=1000*(a[3]-'0')+100*(a[4]-'0')+10*(a[5]-'0')+a[6]-'0';
	if (((0==year%4)&&(0!=year%100))||0==year%400)
	    flag=true;
	else
		flag=false;
	if (flag)
	{
		if (2==month)
		{
			cout<<29<<endl;;
		} 
		else if (1==month||3==month||5==month||7==month||8==month||10==month||12==month)
		{
			cout<<31<<endl;
		}
		else
		{
			cout<<30<<endl;
		}
	}
	else
	{
		if (2==month)
		{
			cout<<28<<endl;;
		} 
		else if (1==month||3==month||5==month||7==month||8==month||10==month||12==month)
		{
			cout<<31<<endl;
		}
		else
		{
			cout<<30<<endl;
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值