zoukankan      html  css  js  c++  java
  • HDU4515 小Q系列故事——世界上最遥远的距离 模拟

    题意:告诉某一天,然后往前和后各推一定天数,输出计算之后的日期。

    解法:一天一天模拟,日期的在各个位的进制不尽相同。

    代码如下:

    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    int month[13] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    bool leap(int y) {
        return (y%4==0 && y%100!=0) || (y%400==0);
    }
    
    int getdays(int y, int m) {
        if (leap(y) && m == 2) return 29;
        return month[m];
    }
    
    struct Data {
        int y, m, d;
        Data():y(2013),m(3),d(24){}
        const Data & sub(int days);
        const Data & add(int days);
        void show() const;
    };
    
    void Data::show() const {
        printf("%04d/%02d/%02d", y, m, d);    
    }
    
    const Data & Data::sub(int days) { // 每次减去一天
        while (days--) {
            d -= 1;
            if (d == 0) {
                d += getdays(y, m-1);
                m -= 1;
            }
            if (m == 0) {
                m = 12;
                y -= 1;
            }
        }
        return *this;
    }
    
    const Data & Data::add(int days) {
        while (days--) {
            d += 1;
            if (leap(y)&&m==2&&d==29) {}
            else if (d > month[m]) {
                d = 1;
                m += 1;
            }
            if (m == 13) {
                m = 1;
                y += 1;
            }
        }
        return *this;
    }
    
    int main() {
        int T, days;
        scanf("%d", &T);
        while (T--) { 
            Data m1, m2;
            scanf("%d", &days);
            m1.add(days).show();
            printf(" ");
            m2.sub(days).show();
            puts("");
        }
        return 0;    
    }
  • 相关阅读:
    优先队列总结
    CodeForces 567D One-Dimensional Battle Ships
    CodeForces 567D One-Dimensional Battle Ships
    codeforces 1016B. Segment Occurrences
    codeforces 1016B. Segment Occurrences
    poj3249(求最长路)
    poj3249(求最长路)
    poj 2186
    2017年第八蓝桥杯C/C++ A组国赛 —— 第二题:生命游戏
    Fence Repair POJ
  • 原文地址:https://www.cnblogs.com/Lyush/p/2978230.html
Copyright © 2011-2022 走看看