【王道机试】模拟-日期累加-北理工

描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入描述:
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
输出描述:
输出m行,每行按yyyy-mm-dd的个数输出。
示例1
输入:
1
2008 2 3 100
输出:2008-05-13

要注意加上若干天后是否已经超过该年的总天数。

#include<iostream>
using namespace std;
int ping[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int run[]={0,31,60,91,121,152,182,213,244,274,305,335,366};

bool isrun(int year){
    if((year % 4==0 && year % 100 != 0)||(year % 400==0)) return true;
    else return false;
}
int main(){
    int m;
    cin>>m;
    while(m--){
        int year,month,day,add,total;
        cin>>year>>month>>day>>add;
        if(isrun(year)){                  //确定年
            total=run[month-1]+day+add;
            while(total>366){
                year++;
                month=1;
                total-=366;
            }
        }else{
            total=ping[month-1]+day+add;
            while(total>365){
                year++;
                month=1;
                total-=365;
            }
        }
        if(isrun(year)){                  //确定月和日
            for(int i=month;i<=12;i++){
                if(total<=run[i]){
                    month=i;
                    total-=run[i-1];
                    printf("%04d-%02d-%02d\n",year,month,total);
                    break;
                }
            }
        }else{
            for(int i=month;i<=12;i++){
                if(total<=ping[i]){
                    month=i;
                    total-=ping[i-1];
                    printf("%04d-%02d-%02d\n",year,month,total);
                    break;
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值