您的位置:首页 > 其它

第九周项目4-输入年月,输出本月天数

2013-10-15 20:41 274 查看
/*
*程序的版权和版本声明部分:
*Copyright(c)2013,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2013年 10月15 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述:输入年月,显示本月多少天
*问题描述:输入年月,显示本月多少天
*程序输出:本月天数
*问题分析:
*算法设计: 先判断是否闰年
*/
我的程序:
#include<iostream>
using namespace std;
int main()
{
int year,month,day;
cout<<"请输入年,月:"<<endl;
cin>>year>>month;
if(year%4==0&&year%100!=0||year%400==0)    //先讨论是不是闰年
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
else if(month==4||month==6||month==9||month==11)
day=30;
else
day=29;            //闰年2月有29天
}
else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
else if(month==4||month==6||month==9||month==11)
day=30;
else
day=28;
}
cout<<"本月有"<<day<<"天"<<endl;
return 0;
}

上机内容:练习

运行结果:


心得体会:在解决实际问题时,往往要经过讨论,灵活运用if-else语句。关键头脑中要先有此问题的算法,解法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  条件