The Lucky Week(规律)

The Lucky Week

Edward, the headmaster of the Marjar University, is very busy every day and always forgets the date.

There was one day Edward suddenly found that if Monday was the 1st, 11th or 21st day of that month, he could remember the date clearly in that week. Therefore, he called such week "The Lucky Week".

But now Edward only remembers the date of his first Lucky Week because of the age-related memory loss, and he wants to know the date of the N-th Lucky Week. Can you help him?

Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:

The only line contains four integers Y, M, D and N (1 ≤ N ≤ 109) indicating the date (Y: year, M: month, D: day) of the Monday of the first Lucky Week and the Edward's query N.

The Monday of the first Lucky Week is between 1st Jan, 1753 and 31st Dec, 9999 (inclusive).

Output
For each case, print the date of the Monday of the N-th Lucky Week.

Sample Input
2
2016 4 11 2
2016 1 11 10


Sample Output
2016 7 11
2017 9 11

题意:一个幸运周的定义是这一周的周一是在1号,11号或21号,给出幸运周的日期,求出第n个幸运周。

思路:打表找规律,每400年一循环,然后暴力找到第n个幸运周。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
int x[5]={1,11,21};
int whatday(int d,int m,int y){
    int ans;
    if(m==1||m==2){
        m+=12,y--;
    }
    if((y<1752)||(y==1752&&m<9)||(y==1752&&m==9&&d<3))
        ans=(d+2*m+3*(m+1)/5+y+y/4+5)%7;
    else
        ans=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int a,b,c,n;scanf("%d%d%d%d",&a,&b,&c,&n);n--;
        int ans=0,flag=0,sum1=0;int h=c,h1=0;
        for(int i=b;i<=12;i++){
                if(h1==0){h=c/10;h1=1;}
                else h=-1;
            for(int j=h+1;j<3;j++){
                if(ans==n){flag=1;break;}
                if(whatday(x[j],i,a)==0){ans++;sum1++;b=i;c=x[j];}
            }
            if(flag==1)break;
        }
        if(flag==1){cout<<a<<" "<<b<<" "<<c<<endl;continue;}
        a+=1;int sum2=0;flag=0;int A,B,C;
        for(int i=a;i<a+400;i++){
            for(int j=1;j<=12;j++){
                for(int k=0;k<3;k++){
                    if(ans==n){flag=1;break;}
                    if(whatday(x[k],j,i)==0)
                       {ans++;sum2++;
                       A=i;B=j,C=x[k];}
                }
                if(flag==1)break;
            }
            if(flag==1)break;
        }
        if(flag==1){cout<<A<<" "<<B<<" "<<C<<endl;continue;}
        int src=(n-sum1)/sum2;
        int y=n-sum1-src*sum2,t=a+(src*400);
        if(y==0){
            for(int i=1;i<=12;i++){
                for(int j=0;j<3;j++){
                    if(whatday(x[j],i,y)==0){a=t;b=i;c=x[j];}
                }
            }
            cout<<a<<" "<<b<<" "<<c<<endl;continue;
        }
        int xx=0,flag1=0;
        for(int i=t;xx<y;i++){
            for(int j=1;j<=12&&xx<y;j++){
                for(int k=0;k<3&&xx<y;k++){if(xx==y){flag1=1;break;}
                   if(whatday(x[k],j,i)==0){xx++;a=i;b=j;c=x[k];}
                }if(flag1==1)break;
            }if(flag1==1)break;
        }
        cout<<a<<" "<<b<<" "<<c<<endl;
    }
}



猜你喜欢

转载自blog.csdn.net/lpeaceminusone/article/details/80137254