第八周本周有多少天

本文介绍了一个使用C++编写的程序,该程序能够根据输入的年份和月份计算并输出该月的总天数。通过判断闰年和平年的不同情况来确定2月的天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题及代码:

/*
*Copyright (c) 2014,烟台大学计算机学院
*ALL right reserved
*文件名;test。cpp
*作者;李莉
*完成日期2014年10月20日
*版本号v1.0
*
*问题描述:输入年月,计算并输出这个月的天数
*输入描述:输入年份year和月份month
*程序输出:得到这个月的天数
*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 int year,month,t;
 cout<<"请输入年份和月份:"<<endl;
 cin>>year>>month;
 if ((year%100!=0&&year%4==0)||(year%400==0))
 {
     if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        t=31;
  else if (month==4||month==6||month==9||month==11)
    t=30;
  else
    t=29;
 }
 else
 {
     if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        t=31;
  else  if (month==4||month==6||month==9||month==11)
    t=30;
  else
    t=28;
 }
 cout<<"本月天数为:"<<t<<endl;
    return 0;
}


运行结果:

知识点总结:本来是用switch语句做的,后来又用if else语句做了一遍,自己感觉用if else语句不如用switch语句简单,而且用if else语句做的这个无法排除错误的月份

心得体会:到达目的地的方法是不同的看,别人可能不会在乎你是步行到达,还是开车到达,但是程序员不一样,程序员不仅要到达目的地,还要高效率的到达目的地

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值