Sicily 1791. Philia's Problem

关键在于判断给出的日期是星期几,要自己写一个函数来判断的话就比较麻烦,可以直接调用zeller函数,相当省事。另外注意年月日的进位就没事了。

Run Time: 0.01sec

Run Memory: 304KB

Code length: 1194Bytes

SubmitTime: 2011-12-11 13:35:04

// Problem#: 1791
// Submission#: 1064449
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
using namespace std;

int days[ 13 ] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int zeller( int year, int month, int day ) {
    if ( month < 3 ) {
        year = year - 1;
        month = month + 12;
    }

    int q = year / 100;
    int m = year % 100;
    int ans = ( q / 4 - 2 * q + m + m / 4 + ( 26 * ( month + 1 ) ) / 10 + day - 1 ) % 7;

    return ( ans > 0 ? ans: ans + 7 );

}

int main()
{
    int N;
    int year, month, day;
    int diff;

    scanf( "%d", &N );
    while ( N-- ) {
        scanf( "%d%d%d", &month, &day, &year );
        diff = 7 - zeller( year, month, day );
        if ( diff == 0 )
            diff = 7;

        if ( month == 2 )
            days[ 2 ] = ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ? 29: 28 );
        day += diff;
        if ( day > days[ month ] ) {
            day -= days[ month ];
            if ( month <= 11 )
                month++;
            else {
                month = 1;
                year++;
            }
        }
        printf( month >= 10 ? "%d ": "0%d ", month );
        printf( day >= 10 ? "%d ": "0%d ", day );
        printf( "%d\n", year );
    }

    return 0;

}                                 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值