Codeup墓地—问题 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
#include <stdio.h>
#include <string.h>
int mmonth[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 date[10][20] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int isLeapyear(int year)   //判断是否是闰年
{
    if((year%4==0&&year%100!=0)||(year%400==0))
        return 1;
    else
        return 0;
}
int _month(char m[])
{
    char M[14][15]= {"", "January","February","March","April","May","June","July","August","September","October","November","December"};
    int i;
    for(i=1; i<13; i++)
        if(strcmp(m,M[i])==0)
            break;
    return i;
}
int main()
{
    int day,year;
    char m[20];
    while(scanf("%d %s %d",&day,m,&year)!=EOF)
    {
        int flag=0;
        int d1=31,m1=8,y1=2018;  //2018-8-31是周五
        int month=_month(m);
        int time1=0,time2=0;
        time1+=year*10000+month*100+day;
        time2+=y1*10000+m1*100+d1;
        if(time1>time2)   //如果第一个年份大于第二个年份,则交换
        {
            flag=1;  //flag标识两个日期是否交换
            int tmp;
            tmp=year;
            year=y1;
            y1=tmp;
            tmp=month;
            month=m1;
            m1=tmp;
            tmp=day;
            day=d1;
            d1=tmp;
        }
        int count=0;
        while(year<y1||month<m1||day<d1)  //日期一直执行加1操作,直到年月日均相同
        {
            day++;   //执行天数加1
            if(day==mmonth[month][isLeapyear(year)]+1) //判断日是否等于当年月份最后一天
            {
                month++;
                day=1;
            }
            if(month==13)   //月份是否满12个月
            {
                year++;
                month=1;
            }
            count++;   //统计相差结果
        }
        count%=7;
        int result;   //result计算周几
        if(flag==1)
            result=(count+5)%7;
        else
            result=(12-count)%7;
        printf("%s\n",date[result]);
    }
    return 0;
}

运行结果:

发布了462 篇原创文章 · 获赞 55 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/LY_624/article/details/88805540
今日推荐