3.4小节——问题 B: Day of Week

题目描述:

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.

样例输入

21 December 2012
5 January 2013

样例输出

Friday
Saturday

代码1(公式)

#include<cstdio>
#include<cstring>
/*基姆拉尔森计算公式(Kim larsson calculation formula): 
   按星期一到星期日的顺序;
   week=(D+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; 数组下标从0开始;

   char week[10][10]={{"Monday"},{"Tuesday"},{"Wensday"},{"Thursday"},
    {"Friday"},{"Saturday"},{"Sunday"}} ;

   week=(D+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7+1; 数组下标从1开始;

   char week[10][10]={{""},{"Monday"},{"Tuesday"},{"Wensday"},{"Thursday"},
    {"Friday"},{"Saturday"},{"Sunday"}} ;

   D表示日期 (例:20号);m表示月份;y表示年份;
   把1月和2月看成是上一年的13月和14月; */
char week[10][20]={{"Monday"},{"Tuesday"},{"Wednesday"},{"Thursday"},
    {"Friday"},{"Saturday"},{"Sunday"}} ;
char month[15][20]={{}, {"January"}, {"February"}, {"March"}, {"April"}, {"May"}, 
    {"June"}, {"July"}, {"August"}, {"September"}, {"October"}, {"November"}, {"December"}};
int main(){
    int d,y;
    int i,j,k;
    char m[15];
    while(scanf("%d %s %d",&d,m,&y)!=EOF){
        for(i=0;i<13;i++){
        if(strcmp(month[i],m)==0)k=i;
        }   
        if(k==1||k==2){                     //12月进行修正; 
            k+=12;
            y--;
        }
        j=(d+2*k+3*(k+1)/5+y+y/4-y/100+y/400)%7;
        printf("%s\n",week[j]);                  
    }
    return 0;   
}

代码2(日期差)

#include<cstdio>
#include<cstring>
char week[10][20]={{"Monday"},{"Tuesday"},{"Wednesday"},{"Thursday"},{"Friday"},{"Saturday"},{"Sunday"}};
char monthLen[15][2]={{},{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 monthNam[15][20]={{},{"January"},{"February"},{"March"},{"April"},{"May"},{"June"},{"July"},{"August"},{"September"},
                       {"October"},{"November"},{"December"}};
bool isLeap(int year){
    return (year%4==0&&year%100!=0)||(year%400==0);
}
void swap(int &a,int &b){                       //这是"引用",不是取地址 
    int temp=a;
    a=b;
    b=temp;
} 
int main(){              
    char month[20];
    int d2,m2,y2;
    int i,ans;
    int time1,time2,flag=0;                           //用来比较两个日期大小 
    while(scanf("%d %s %d",&d2,month,&y2)!=EOF){
        int d1=31,m1=8,y1=2018;                       //务必要注意,这个应该定义在里面,后面这可能会变化,所以每次都重新赋值 
        for(i=1;i<13;i++){
            if(strcmp(month,monthNam[i])==0){
                m2=i;
                break;
            } 
        }                                            //确定读入的月,转换成数字形式 
        time1=y1*10000+m1*100+d1;
        time2=y2*10000+m2*100+d2;
        if(time1>time2){
            flag=1;                                 //用flag来标志两者是否交换 
            swap(d1,d2);
            swap(m1,m2);
            swap(y1,y2); 
        }                                          //确保time1小于time2 
        ans=0;
        while(d1<d2||m1<m2||y1<y2){
            d1++;
            if(d1==monthLen[m1][isLeap(y1)]+1){
                m1++;
                d1=1;
            }                  //满当月天数
            if(m1==13){
                y1++;
                //当y1走完当年的天数,我们可以进行大跨步,跨至y1=y2-1;
                while(y1<y2){
                    if(isLeap(y1))ans+=366;
                    else ans+=365;
                    y1++;   
                }
                m1=1;
            } 
            ans++;
        } 
        ans%=7;
        if(!flag){
            ans=(ans+4)%7;
            printf("%s\n",week[ans]);         //2018.08.31是周五,下标在数组中是4;输入的日期大; 
        }else{
            ans=(11-ans)%7;
            printf("%s\n",week[ans]);         //输入的日期小;
            flag=0;
        } 
    }
    return 0;
} 
    原文作者:codeup题目解答
    原文地址: https://blog.csdn.net/weixin_42114379/article/details/82257480
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞