C++日期通用

日期累加

http://www.nowcoder.com/questionTerminal/eebb2983b7bf40408a1360efb33f9e5d

#include <iostream>
using namespace std;

int months_avg[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int months_leap[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

#define isLeapYear(y) ((year%4==0 && year%100!=0)||year%400 ==0)

int main()
{
    int m,year,month,day,days;
    cin >> m;
    while (m>0) {
        cin >> year >> month >> day >> days;
        int *months = isLeapYear(year)?months_leap:months_avg;
        while (days > 0) {
            if (day + days <= months[month-1] ) {
                day += days;            
                days -= days;
            }
            else {
                days -= (months[month-1] - day + 1);
                day = 1;
                month +=1;
                if (month>12) {
                    year++;
                    months = isLeapYear(year)?months_leap:months_avg;
                    month = 1;
                }
            }
        }    
        printf("%04d-%02d-%02d\n", year,month,day);
        m--;
    }
    return 0;
}
全部评论

相关推荐

我朋友的华子2012,HR已经开始问意向地区了,好急
不讲武德的黑眼圈很能干:急得不行 也不说评级 不知道报的多少啊😡
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务