zoukankan      html  css  js  c++  java
  • 25:计算两个日期之间的天数

    25:计算两个日期之间的天数

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    给定两个日期,计算相差的天数。比如2010-1-1和2010-1-3相差2天。

    输入
    共两行:
    第一行包含三个整数startYear,startMonth,startDay,分别是起始年、月、日。
    第二行包含三个整数endYear,endMonth,endDay,分别是结束年、月、日。
    相邻两个整数之间用单个空格隔开。

    年份范围在1~3000。保证日期正确且结束日期不早于起始日期。
    输出
    输出一个整数,即是两个日期相差的天数。
    样例输入
    2008 1 1
    2009 1 1
    样例输出
    366
    提示
    闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。闰年的2月份有29天。
     1 #include<iostream>
     2 using namespace std;
     3 int bgyear,bgmonth,bgday;
     4 int enyear,enmonth,enday;
     5 int month[21]={0,31,28,31,30,31,30,31,31,30,31,30,31};//非闰年
     6 int rmonth[21]={0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年 
     7 int flag=1;
     8 int tot=0;
     9 int main()
    10 {
    11     cin>>bgyear>>bgmonth>>bgday;
    12     cin>>enyear>>enmonth>>enday;
    13     for(int i=bgyear;i<=enyear+1;i++)//寻找年数上的差异 
    14     {
    15         if((i%4==0&&i%100!=0)||(i%400==0))
    16         {
    17             for(int j=1;j<=12;j++)
    18             {
    19                 if(i==bgyear&&j<bgmonth)
    20                 continue;//寻找开始月份
    21                 for(int k=1;k<=rmonth[j];k++)
    22                 {
    23                     if(i==enyear&&j==enmonth&&k==enday)
    24                     {
    25                         cout<<tot;
    26                         return 0;
    27                     }
    28                     if(k<bgday&&flag==1)
    29                     continue;
    30                     else
    31                     {
    32                         flag=0;
    33                         tot++;
    34                     }
    35                     
    36                 }
    37                  
    38             }
    39         }//闰年 
    40         else
    41         {
    42         
    43             for(int j=1;j<=12;j++)
    44             {
    45                 if(i==bgyear&&j<bgmonth)
    46                 continue;//寻找开始月份
    47                 for(int k=1;k<=month[j];k++)
    48                 {
    49                     if(i==enyear&&j==enmonth&&k==enday)
    50                     {
    51                         cout<<tot;
    52                         return 0;
    53                     }
    54                     if(k<bgday&&flag==1)
    55                     continue;
    56                     else
    57                     {
    58                         flag=0;
    59                         tot++;
    60                     }
    61                     
    62                 }
    63                  
    64             }
    65         }//非闰年 
    66     }
    67     cout<<tot;
    68     return 0;
    69 }
  • 相关阅读:
    Win7 VSCode 在线安装Rust语言及环境配置
    Win7 VSCode 离线安装Rust语言及环境配置
    Win7崩溃程序目录
    fatal error C1047: The object or library file xxx was created with an older compiler than other objects
    Notepad++正则表达式合并多行代码为1行
    Win7 VS2019安装后创建C++工程失败解决
    关于Visual Studio中书签Bookmark的一些问题
    Fira Code,可以让不等号!=直接显示出来的字体
    免费商用字体
    Win7 64位注册32位DLL
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6538980.html
Copyright © 2011-2022 走看看