一直很害怕這類題目,而且這類題目還算是送分的題目、、、、
寫完這題,發現自己的邏輯思維能力確實太差,WA 二十幾發,後來發現是 一個小錯誤、、 - -b
記得有幾次比賽出現求日期的題,我都是直接放棄的,這次強壓着自己、、、
YYYY MM DD
W
1 <= MM <= 12
1 <= DD <= 31,且確保測試樣例中YYYY年MM月DD日是一個合理日期
1 <= W <= 7,分別代表週一到週日
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
#define N 3010
bool tt[N];
int sg[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void judge()
{
for(int i = 1599; i <= 2999; i ++)
{
if( i % 4 == 0 && i % 100 != 0 ||(i % 400 == 0))
tt[i] = true;
}
}
int main()
{
int yy, mm, dd;
memset(tt, false, sizeof(tt));
judge();
while(~scanf("%d%d%d",&yy,&mm,&dd))
{
int ans = 0;
if(yy > 2011)
{
ans += 50;
for(int i = 2012; i <= yy - 1; i ++)
{
if(tt[i])
ans += 366;
else
ans += 365;
}
if(mm >= 3 && tt[yy])
ans ++;
for(int i = 1; i <= mm - 1; i ++)
{
ans += sg[i];
}
ans += dd;
int tmp_1 = ((ans) % 7 + 5)%7;
if(tmp_1 != 0)
{
printf("%d\n",tmp_1);
}
else
printf("7\n");
continue;
}
else if(yy == 2011)
{
if(mm < 11)
{
for(int i = mm; i <= 10; i ++)
{
ans += sg[i];
}
ans += 11;
ans -= dd;
}
else if(mm > 11)
{
ans += (19 + dd);
}
else
ans += dd - 11;
int tmp_1 = (ans + 12) % 7;
if(tmp_1 != 0)
{
printf("%d\n",tmp_1);
}
else
printf("7\n");
continue;
}
else
{
for(int i = yy + 1; i < 2011; i ++)
{
if(tt[i])
ans += 366;
else
ans += 365;
}
int tmp_2 = 0;
for(int i = 1; i <= mm - 1; i ++)
{
tmp_2 += sg[i];
}
if(mm >= 3 && tt[yy])
tmp_2 ++;
tmp_2 += dd;
ans += (365 - tmp_2);
if(tt[yy])
ans ++;
for(int i = 1; i < 11; i ++)
{
ans += sg[i];
}
ans += 11;
int tmp_1 = abs(ans % 7 - 5)%7 ; // 死在這裏、、、、一開始 寫的是 (ans + 5)%7;
if(tmp_1 != 0)
{
printf("%d\n",tmp_1);
}
else
printf("7\n");
continue;
}
}
}
/****
2011 11 15
2011 11 20
2011 12 30
2010 11 12
1599 1 1
1599 3 1
2015 2 24
2037 3 30
1980 2 29
1980 3 21
****/