关键在于判断给出的日期是星期几,要自己写一个函数来判断的话就比较麻烦,可以直接调用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;
}