zoukankan      html  css  js  c++  java
  • 1037:18岁生日(日期计算问题)

    题目描述

    小明的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达18岁生日所经过的总天数,让他好来比较一下。

    输入格式

    输入的第一行是一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。

    输出

    T行,每行一个数,表示此人从出生到18岁生日所经过的天数。如果这个人没有18岁生日,就输出-1。

    样例输入

    1
    1988-03-07

    样例输出

    6574

    思路:计算十八岁的生日距离出生日期有多少天。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 bool ifLeap(int year);//判断是否闰年
     6 int getAdDays(int year,int month,int day);//判断日期是公元后第几天?从0000.01.01天开始
     7 
     8 int mDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
     9 
    10 int main(){
    11     int T;
    12     char c;
    13     cin>>T;
    14     while(T--){
    15         int y,m,d;
    16         cin>>y>>c>>m>>c>>d;
    17         if(m==2&&d==29)cout<<-1<<endl;
    18         else{
    19             cout<<getAdDays(y+18,m,d)-getAdDays(y,m,d)<<endl;
    20         }
    21     }
    22     return 0;
    23 }
    24 bool ifLeap(int year){
    25     if(year%400==0 ||(year%100!=0 && year%4==0))
    26         return true;
    27     return false;
    28 }
    29 
    30 int getAdDays(int year,int month,int day){
    31     int aYear = 0,aMonth = 1,aDay = 1,days = 0,i;
    32     for(i=1;i<=year;i++){
    33         days+=365;
    34         if(ifLeap(i-1))
    35             days+=1;
    36     }
    37     for(i=1;i<month;i++){
    38         days+=mDays[i-1];
    39     }
    40     if(ifLeap(year) && month>2)
    41         days+=1;
    42     days+=day;
    43     return days;
    44 }
  • 相关阅读:
    Anagram
    HDU 1205 吃糖果(鸽巢原理)
    Codeforces 1243D 0-1 MST(补图的连通图数量)
    Codeforces 1243C Tile Painting(素数)
    Codeforces 1243B2 Character Swap (Hard Version)
    Codeforces 1243B1 Character Swap (Easy Version)
    Codeforces 1243A Maximum Square
    Codeforces 1272E Nearest Opposite Parity(BFS)
    Codeforces 1272D Remove One Element
    Codeforces 1272C Yet Another Broken Keyboard
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/4078775.html
Copyright © 2011-2022 走看看