算星期几,找当前日期当基准,但要注意向前和向后有区别,利用flag区分

问题 B: Day of Week

[命题人 : 外部导入]

时间限制 : 1.000 sec  内存限制 : 32 MB
 

题目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

样例输入 Copy

21 December 2012
5 January 2013

样例输出 Copy

Friday
Saturday
#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
//巧用二维数组保存平润二月
int month_num[13][2]={
   
   {0,0},{31,31},{28,29},{31,31},{30,30},
                        {31,31},{30,30},{31,31},{31,31},
                        {30,30},{31,31},{30,30},{31,31}};
char week[8][10]={"Monday","Tuesday","Wednesday","Thursday",
                    "Friday","Saturday","Sunday","Monday"};
char month_name[13][10]={"","January","February","March","April",
                            "May","June","July","August",
                            "September","October","November","December"};
int isrun(int y){
    if(y%400==0||y%4==0&&y%100!=0)
        return 1;
    return 0;
}
int find_month(char str[]){
    for(int i=1;i<13;i++)
        if(!(strcmp(str,month_name[i])))
            return i;
    return -1;
}
int main(){
    char str[20],M[10];
    int d,y;
    while(gets(str)!=NULL){
        sscanf(str,"%d %s %d",&d,M,&y);
        int m=find_month(M),ans=0;
        //printf("%d %d %d\n",d,m,y);
        int a=y*10000+m*100+d,b=20210104,flag=1;
        //printf("a:%d\n",a);
        if(a>=b){
            int temp=a;a=b;b=temp;
            flag=0;
        }
        int y1=a/10000,m1=a/100%100,d1=a%100;
        int y2=b/10000,m2=b/100%100,d2=b%100;
        //printf("%d %d %d\n%d %d %d\n",y1,m1,d1,y2,m2,d2);
        while(y1!=y2||m1!=m2||d1!=d2){//利用进位算天数
            d1++;
            if(d1==month_num[m1][isrun(y1)]+1){
                d1=1;
                m1++;
            }
            if(m1==13){
                y1++;
                m1=1;
            }
            ans++;
        }
        //printf("%d %d %d\n",ans,(ans)%7,flag);
        if(flag==1)
            printf("%s\n",week[7-(ans)%7]);
        else
            printf("%s\n",week[(ans)%7]);
    }
    return 0;
}
/*
8 January 2021

4 January 2021
*/

猜你喜欢

转载自blog.csdn.net/ur_ytii/article/details/112346937
今日推荐