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;
}
全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务