【问题描述】
输入年份和月份,输出该月份的天数。
【输入形式】
输入一行,两个整数,分别为年份和月份。
【输出形式】
一个整数,表示该月份的天数
【样例输入】
2018 3
【样例输出】
31
#include <iostream>
using namespace std;
int calculateYear(int year,int month){
if(year%400==0||(year%100!=0&&year%4==0)){ //闰年的二月29天
if (month == 2){
return 29;
}
else{
if(month == 1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
}
else return 30;
}
}
else {
if (month == 1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
}
if (month == 2)return 28;
else return 30;
}
}
int main(){
int a,b;
cin>>a>>b;
int sum = calculateYear(a,b);
cout<<sum;
return 0;
}
本文介绍了一个C++程序,用于根据输入的年份和月份计算并输出该月份的天数,特别考虑了闰年的规则。函数`calculateYear`根据输入判断并返回相应月份的天数,`main`函数作为入口处理用户输入并显示结果。
376

被折叠的 条评论
为什么被折叠?



