现在的位置: 首页 > 综合 > 正文

杂七杂八 – 日期计算

2019年02月19日 ⁄ 综合 ⁄ 共 1584字 ⁄ 字号 评论关闭

    一直很害怕这类题目,而且这类题目还算是送分的题目、、、、

    写完这题,发现自己的逻辑思维能力确实太差,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

****/

抱歉!评论已关闭.