Sicily 1814. 日期计算问题

来源:互联网 发布:收银软件免费版 编辑:程序博客网 时间:2024/06/05 21:51

还是写类

时间分三部分. 大年过了多少天, 小年还剩多少天, 中间多少天.

// Problem#: 1814// Author#: Reid Chan#include <iostream>#include <string>using namespace std;int months[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };class time_y {private:    int change_to_int(string t) {        int len = t.length();        int sum = 0;        int tmp = 1;        for (int i = len - 1; i >= 0; i--) {            sum += tmp * (t[i] - '0');            tmp *= 10;        }        return sum;    }public:    int year;    int month;    int day;    time_y(string time) {        year = change_to_int(time.substr(0, 4));        month = change_to_int(time.substr(5, 2));        day = change_to_int(time.substr(8, 2));    }};bool isY(int y) {    if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) {        return true;    }    return false;}int cal_day(time_y t, bool pre) {    int day = 0;    int end = t.month;    for (int i = 1; i < end; ++i) {        if (i == 2 && isY(t.year)) {            day += months[0];            continue;        }        day += months[i];    }    day += t.day;    if (pre) { return isY(t.year) ? 366 - day : 365 - day; }    else { return day; }}int cal_year(int bgn, int end) {    int day = 0;    for (int i = bgn + 1; i < end; ++i) {        if (isY(i)) { day += 366; }        else { day += 365; }    }    return day;}int main() {    int t;    cin >> t;    while(t--) {        string a, b;        cin >> a >> b;        time_y t1(a), t2(b);        int result = 0;        if (t1.year > t2.year) {            result += cal_day(t2, true);            result += cal_day(t1, false);            result += cal_year(t2.year, t1.year);        } else if (t1.year < t2.year) {            result += cal_day(t1, true);            result += cal_day(t2, false);            result += cal_year(t1.year, t2.year);        } else {            int r1 = cal_day(t1, false);            int r2 = cal_day(t2, false);            result = (r1 - r2 > 0) ? r1 - r2 : r2 - r1;        }        cout << result << endl;    }    return 0;}                                 


0 0