[日期類問題] 例 2.4 Day of week(九度教程第 7 題)

題目

時間限制:1 秒 內存限制:32 兆 特殊判題:否

題目描述:
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.
樣例輸入:
9 October 2001
14 October 2001
樣例輸出:
Tuesday
Sunday
來源:
2008 年上海交通大學計算機研究生機試真題

分析

在上題的基礎上,只需要改進如下:
1. 知道今天是星期幾
2. 計算日期與今天的差值
3. 求出日期是星期幾

代碼

#include<iostream>
#include<string.h>
#define ISLEAPYEAR(x) x % 100 != 0 && x % 4 == 0 || x % 400 == 0 ? 1 : 0 //簡潔寫法 
using namespace std;

int daysOfMonth[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 monthName[13][20] = 
{
    "",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November", 
    "December"
}; //月名 每個月名對應下標1到12

char weekName[8][20] = 
{
    "",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
}; //周名 每個周名對應下標1到7

class Date //注意不要加括號 
{
    public:
        int year;
        int month;
        int day;
        Date() //初始化日期爲0/1/1 作爲基準 
        {
            year = 0;
            month = 1; 
            day = 1;
        }
        void update() //將日期加一天 
        {
            day++;
            if(day > daysOfMonth[month][ISLEAPYEAR(year)])
            {
                day = 1;
                month ++;
                if(month > 12)
                {
                    month = 1;
                    year ++;
                }
            }
        }
};

int Abs(int x)
{
    return (x >= 0 ? x : -1*x); 
}

int buf[6001][13][32]; //用來存儲每個日期與基準日期的差值 

int main()
{
    Date temp;
    int count = 0;
    while(temp.year < 6000)
    {
        buf[temp.year][temp.month][temp.day] = count;
        temp.update();
        count ++;
    }
    int d, m, y;
    char s[20];

    while(cin >> d >> s >> y) //此處注意對於格式的控制 
    {
        for(m = 1; m <= 12; m ++)
            if(strcmp(s, monthName[m]) == 0) break;
        int gap = buf[y][m][d] - buf[2016][11][21];
        if(gap >= 0) 
            cout << weekName[(gap % 7 + 1)] << endl;
        else 
            cout << weekName[(gap % 7 + 7 + 1)] << endl;
    }
    return 0;
}

總結

  1. day > daysOfMonth[month][ISLEAPYEAR(year)] 取數組元素的時候不要和函數混了
  2. 注意此題中數組的使用,很方便,另外數組的序號也很講究
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章