zoukankan      html  css  js  c++  java
  • Mother's Day 哈尔滨工程大学ACM预热赛 (基姆拉尔森计算公式)

    链接:https://ac.nowcoder.com/acm/contest/554/E
    来源:牛客网
     

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    Mother's Day is a celebration honoring the mother of the family, as well as motherhood, maternal bonds, and the influence of mothers in society, which is celebrated on various days in many parts of the world, but mostly, the date is on the second Sunday in May. 
        Like Mother's Day, there are also several celebrations have no fixed date, but are set in the form of "the Bth(st/nd) weekday C of the Ath(st/nd) month in a year", for example, as above, Mother's Day is on the second Sunday in May of a year, but also can be described as "the 2nd weekday 7 of the 5st month in a year"(A=5, B=2, C=7). 
        Now, to help remember these special days, HVT wants you to calculate all the exact date of such celebrations in the past and future hundreds of years, which means that you are given 4 numbers: A(1 ≤ A ≤ 12), B(B ≥ 1), C(1 ≤ C ≤ 7, 1=Monday,2=Tuesday,...,7=Sunday) and y(represents the year, 1850 ≤ y ≤ 2050), and you should write code to calculate the exact date of "the Bth(st/nd) weekday C of the Ath(st/nd) month in a year y".
        For your convenience, we will noice you that: January 1st, 1850 is a Tuesday.

    输入描述:

    The input contains multiple lines of test data, each line has 4 numbers: A B C and y.

    输出描述:

    For each input, you should output a exact date in the format of "yyyy/mm/dd"(When the number of digits is insufficient, 0 should be added to the front);
    if the Bth(st/nd) weekday C of the Ath(st/nd) month in a year y does not exist(for example, there will never be a 7th Monday in any months of any year), you should output "none" (without quotes).

    示例1

    输入

    复制

    4 2 7 2018
    4 1 7 2018
    2 5 4 2018
    2 4 3 2018

    输出

    复制

    2018/04/08
    2018/04/01
    none
    2018/02/28

    直接套公式

    暴力枚举即可

    #include<cstdio>
    #include<iostream>
    using namespace std;
    int cal(int y,int m, int d)
    {
        if(m==1||m==2) {
            m+=12;
            y--;
        }
        int iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
      	return iWeek;
    }
    int run(int x)
    {
        if(x%4==0&&(x%100!=0||x%400==0)) return 1;
        return 0;
    }
    int month[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int main()
    {
        int A,B,C,Y;
        while(~scanf("%d%d%d%d",&A,&B,&C,&Y))
        {
            int tmp;
            tmp= (run(Y)==1&&A==2)?month[A]+1:month[A];
            int cnt=0;
            int flag=0;
            //cout<<tmp<<"}"<<endl;
            for(int i=1;i<=tmp;i++)
            {
                //cout<<cal(Y,A,i)<<"&"<<endl;
                if(cal(Y,A,i)+1==C) cnt++;
                if(cnt==B)
                {
                    flag=1;
                    printf("%04d/%02d/%02d
    ",Y,A,i);
                    break;
                }
            }
            if(flag==0) puts("none");
        }
    }
    
  • 相关阅读:
    SPFA算法——香甜的黄油
    并查集及应用
    背包问题——装箱问题(洛谷1049)用true模板
    划分类动态规划——乘积最大(洛谷1018,codevs1017)
    多源最短路Floyed——多源最短路(CODEVS1077)(可能Floyed模板)
    并查集——亲戚(洛谷1551)(可做并查集模板)
    最小生成树Kruskal——最优布线问题(codevs 1231) (可做Kruscal模板)
    最小生成树Prim——最短网络(洛谷1546)(USACO 3.1.1)可当Prim模板
    lsy263 の CSP-S 2019游记
    About Me
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852288.html
Copyright © 2011-2022 走看看