输入含年月的字符串,仅支持信息格式为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;
}