美文网首页
日期——2. Day of Week

日期——2. Day of Week

作者: 辘轳鹿鹿 | 来源:发表于2020-06-23 17:52 被阅读0次

上海交通大学复试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 2005, 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.
Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

示例1

输入

9 October 2001
14 October 2001

输出

Tuesday
Sunday

解题心得:

  • 这道题与之前日期插值的思路比较相似
  • 编写程序的时候数组的内存溢出了,导致程序暂停了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define isrun(x) (x%4==0&&x%100!=0)||(x%400==0)?1:0

struct Date{
    int year;
    int month;
    int day;
}tmp;
int buf[3001][13][32];
int MonthOfDay[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};
void NextDAY(){
    tmp.day++;
    if(tmp.day>MonthOfDay[tmp.month][isrun(tmp.year)]){
        tmp.day=1;
        tmp.month++;
    }
    if(tmp.month>12){
        tmp.month=1;
        tmp.year++;
    }
}


int main()
{
    int d,m,y,days;
    char mm[20];
    char MonthName[13][20]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
    char WeekName[7][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int cnt=0;
    tmp.year=0;
    tmp.month=1;
    tmp.day=1;
    while(tmp.year!=3001){
        buf[tmp.year][tmp.month][tmp.day]=cnt;
        NextDAY();
        cnt++;
    }


    while(scanf("%d %s %d",&d,mm,&y)!=EOF){

        int i;
        for(i=1;i<=12;i++){
            if(strcmp(MonthName[i],mm)==0)
                break;
        }

        days=buf[y][i][d]-buf[2020][6][22];//2020/6/22是星期一
        days=days+1;

        printf("%s\n",WeekName[((days%7)+7)%7]);
    }
    return 0;
}

相关文章

网友评论

      本文标题:日期——2. Day of Week

      本文链接:https://www.haomeiwen.com/subject/aqvhfktx.html