WARush

SRMの結果とか、解けた問題のコードを書いていきます

Codeforces #183 Div2 B "Calendar"

問題

http://codeforces.com/contest/304/problem/B

日付というものは、今日では事実上の国際基準であるグレゴリアン暦が広い地域で使われていて、市民の為に、世界のほぼ全てで使われている。グレゴリアンはジュリアン暦のうるう年の形式を、次のように改正したおのである。

4で割り切れる年はうるう年である。
しかし100で割り切れたらうるう年ではない。
しかし、400で割り切れたらうるう年である。

課題は、2つの日付が与えられるので、その間、何日あるか計算する事である。うるう年の2月は普段とは違う日数である事に注意せよ。

制約

1900 <= 年 <= 2038


考えた事

実装が大変や


ソースコード

// うるう年か?
bool isLeapYear( int year ){
    if( year % 400 == 0 ) return true;
    if( year % 100 == 0 ) return false;
    if( year % 4 == 0 ) return true;
    return false;
}

// この月の日数は?
int dayOfMonth( int year, int month ){
    int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    return days[month] + ( month == 2 && isLeapYear(year) ? 1 : 0 );
}

// この年の日数は?
int dayOfYear( int year ){
    return isLeapYear(year) ? 366 : 365;
}

// 1900年1月1日からの日数を返す
int getDays( int year, int month, int day ){
    int y = 1900;
    int m = 1;

    int res = 0;
    while( y < year ){
        res += dayOfYear( y );
        y++;
    }

    while( m < month ){
        res += dayOfMonth( y, m );
        m++;
    }

    return res + day - 1;
}

int main() {
    int sy, sm, sd;
    string sstr;
    cin >> sstr;
    sscanf( sstr.c_str(), "%d:%d:%d", &sy, &sm, &sd );
    int ey, em, ed;
    string estr;
    cin >> estr;
    sscanf( estr.c_str(), "%d:%d:%d", &ey, &em, &ed );

    cout << abs((getDays( ey, em, ed ) - getDays( sy, sm, sd ))) << endl;
}