雜七雜八 - 日期計算

    一直很害怕這類題目,而且這類題目還算是送分的題目、、、、

    寫完這題,發現自己的邏輯思維能力確實太差,WA 二十幾發,後來發現是 一個小錯誤、、 - -b

    記得有幾次比賽出現求日期的題,我都是直接放棄的,這次強壓着自己、、、


    

問題描述
  已知2011年11月11日是星期五,問YYYY年MM月DD日是星期幾?注意考慮閏年的情況。尤其是逢百年不閏,逢400年閏的情況。
輸入格式
  輸入只有一行
  YYYY MM DD
輸出格式
  輸出只有一行
  W
數據規模和約定
  1599 <= YYYY <= 2999
  1 <= MM <= 12
  1 <= DD <= 31,且確保測試樣例中YYYY年MM月DD日是一個合理日期
  1 <= W <= 7,分別代表週一到週日
樣例輸入
2011 11 11
樣例輸出
5

 我想說 又被我寫的非常的複雜、、、、其實就是簡單的模擬、、、
    
#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

****/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章