NYOJ 219 An problem about date

简介: 点击打开链接NYOJ 219 1题目:                                                                  An problem about date 描述     acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你能写个程序帮帮他。

点击打开链接NYOJ 219


1题目:
                                                                 An problem about date
描述
    acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你能写个程序帮帮他。
输入
    每行有三个整数 year,month,day,日期在1600年1月1日到9600年1月1日之间;
输出
    输出对应的星期,用一个整数表示;(星期一到星期六用1-6表示,星期日用0表示)
样例输入
    2011 3 6
    1949 10 1
    2011 4 1
    1945 8 15
样例输出
    0
    6
    5
    3

2蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
         1公式中的符号含义如下:w:星期几;c:世纪-1;y:年(后面两位数);m:月(m 大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算);d:日;[ ]代表取整,即只要整数部分。
         2C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和 14月来算,这时C和y均按上一年取值.
         3算出来的W除以7,余数是几就是星期几。如果余数是0,则为星期日。如果于数为负数,那么还要加上7.
         4以2049年10月1日(100周年国庆)为例,用蔡勒(Zeller)公式进行计算,过程如下:
           蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
                                                    =49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1
                                                    =49+[12.25]+5-40+[28.6]
                                                    =49+12+5-40+28
                                                    =54 (除以7余5)
                                                    即2049年10月1日(100周年国庆)是星期5。
3代码:

#include <algorithm>  
#include <iostream> 
#include <cstring>
#include <cstdio>
using namespace std;
#define MAXN 100010
#define INF 0xFFFFFFF

int c , year , month , date;

void solve() {
    int ans , w;
    if(month <= 2){/*如果当前的月数为1 2月那么year-- ,month+=12*/
        year-- ; month += 12;
    }
    c = year/100 ; year = year%100;
    w = year+(year/4)+(c/4)-2*c+(26*(month+1)/10)+date-1;
    ans = w%7 ; if(ans < 0) ans += 7;/*如果当前的ans小于0,那么ans+=7*/
    printf("%d\n" , ans);
}

int main() {
    //freopen("input.txt", "r", stdin);
    while(scanf("%d%d%d" , &year , &month , &date) != EOF)
        solve();
    return 0;
}



目录
相关文章
|
Unix 关系型数据库 MySQL
Year 2038 problem
Year 2038 problem
76 0
|
6月前
|
编译器 API C++
【Matlab】解决使用Mex 报错There was a problem creating the mex file for Real Time Execution ,Please ensure y
解决Matlab使用Mex时出现的"Real Time Execution"错误的步骤,即通过安装"MATLAB 支持 MinGW-w64 C/C++ 编译器"这个包来确保编译器设置正确。
100 0
|
9月前
Date的Invalid Date
Date的Invalid Date
61 1
[USACO 2021.02 Feb]Problem 1. Year of the Cow
[USACO 2021.02 Feb]Problem 1. Year of the Cow
101 0
HDOJ 2058 The sum problem
HDOJ 2058 The sum problem
124 0
HDOJ 1001Sum Problem
HDOJ 1001Sum Problem
126 0
lecture 2.2 problem set 1 and 2
1 COUNTING VOWELS   (10/10 分数) Assume s is a string of lower case characters.
1052 0
lecture 3.2 problem set 3
"Radioactive decay" is the process by which an unstable atom loses energy and emits ionizing particles - what is commonly refered to as radiation.
1158 0