王道论坛计算机考研机试指南 二 日期类问题

二日期类问题

例2.3 日期差值 (九度教程第6题)
时间限制:1秒 **内存限制:32兆 ** 特殊判题:否

题目描述:
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出:
每组数据输出一行,即日期差值

样例输入:
20110412
20110422

样例输出:
11

来源:
2009年上海交通大学计算机研究生机试真题

#include 
#define ISYEAP(x) x%100 != 0 && x%4 ==0 || x%400 == 0 ? 1:0
// 定义宏判断是否是闰年,方便计算每月天数
int dayOfMonth [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
}; // 预存每天的天数,注意二月配合宏定义做特殊处理
struct Date {
    int Day;
    int Month;
    int Year;
    void nextDay() {
        Day ++;
        if (Day > dayOfMonth[Month][ISYEAP(Year)]) { //若天数超过了当月最大日数 
            Day = 1;
            Month ++; // 进入下一月 
            if(Month > 12) { //月数超过12
            Month = 1;
            Year ++; // 进入下一年 
            } 
        }
    } 
};
 
int buf[5001][13][32]; //保存预处理的天数
int Abs(int x) { // 求绝对值
    return x < 0 ? -x: x; 
} 
int main () {
    Date tmp;
    int cnt = 0; //天数计数
    tmp.Day = 1;
    tmp.Month = 1;
    tmp.Year = 0; //初始化日期类对象为0年1月1日
    while (tmp.Year != 5001) { //日期类不超过5000年
        buf[tmp.Year][tmp.Month][tmp.Day] = cnt; // 将该日与0年1月1日的天数差保存起来 
        tmp.nextDay(); //计算下一天日期
        cnt ++;
    } 
    int d1,m1,y1;
    int d2,m2,y2;
    while (scanf ("%4d%2d%2d",&y1,&m1,&d1) != EOF) { 
        scanf ("%4d%2d%2d",&y2,&m2,&d2); //读入要计算的两个日期
        printf("%d\n",Abs(buf[y2][m2][d2]-buf[y1][m1][d1])+1);//用预处理的数据计算两日期差值,注意需对其求绝对值
    }
    return 0;
}
例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
提示:
Month and Week name in Input/Output:January, February, March, April, May, June, July, August, September, October, November, DecemberSunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
**来源: **
2008 年上海交通大学计算机研究生机试真题

#include 
#include 
#define ISYEAP(x) x % 100 != 0 && x % 4 == 0 || x % 400 == 0 ? 1 : 0
int dayOfMonth[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
    };
struct Date {
    int Day;
    int Month;
    int Year;
    void nextDay () {
        Day ++;
        if (Day > dayOfMonth[Month][ISYEAP(Year)]) {
            Day = 1;
            Month ++;
            if (Month > 12) {
                Month = 1;
                Year ++;
            }
        }   
    }
};
int buf[3001][13][32];
char monthName[13][20]={
    "",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
    };//月名 每个月名对应下标1到12
    char weekName[7][20] = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
    };//周名 每个周名对应下标0到6
int main() {
    Date tmp;
    int cnt = 0;
    tmp.Day = 1;
    tmp.Month = 1;
    tmp.Year = 0;
    while (tmp.Year != 3001) {
        buf[tmp.Year][tmp.Month][tmp.Day] = cnt;
        tmp.nextDay();
        cnt ++;
    }//以上与上题一致,预处理出每一天与原点日期的天差数
    int d, m, y;
    char s[20];
    while(scanf("%d%s%d",&d,s,&y)!=EOF) {
        for (m = 1; m <= 12; m++) {
            if(strcmp(s,monthName[m])==0) {
                break;//将输入字符串与月名比较得出月数
            }
        }
    int days = buf[y][m][d]-buf[2012][7][16];//计算给定日期与今日日期的天数间隔(注意可能为负)
    days += 1;//今天(2012.7。16)为星期一,对应数组下标为1,则计算1进经过days天后的下标 
    puts(weekName[(days%7+7)%7]);//将计算后得出的下标用7对其取模,并且保证其为非负数,则该下标极即为答案所对应的下标,输出即可 
    } 
    return 0;   
} 
例2.4 今年的第几天? (九度教程第8题)
时间限制:1秒 **内存限制:32兆 ** 特殊判题:否

题目描述:
输入年、月、日,计算该天是本年的第几天。

输入:
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。

输出:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。

样例输入:
1990 9 202000 5 1

样例输出:
263122

来源:
2003年清华大学计算机研究生机试真题

#include 
#define ISYEAP(x) x%100 != 0 && x%4 ==0 || x%400 == 0 ? 1:0
int dayOfMonth [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
};
struct Date {
    int Day;
    int Month;
    int Year;
    void nextDay() {
        Day ++;
        if (Day > dayOfMonth[Month][ISYEAP(Year)]) { 
            Day = 1;
            Month ++; 
            if(Month > 12) { 
            Month = 1;
            Year ++; 
            } 
        }
    } 
};

int buf[5001][13][32]; 
int Abs(int x) { 
    return x < 0 ? -x: x; 
} 
int main () {
    Date tmp;
    int cnt = 0; 
    tmp.Day = 1;
    tmp.Month = 1;
    tmp.Year = 0; 
    while (tmp.Year != 5001) { 
        buf[tmp.Year][tmp.Month][tmp.Day] = cnt; 
        tmp.nextDay();
        cnt ++;
    } 
    int d1,m1,y1;
    int d2,m2,y2;
    while (scanf ("%4d%2d%2d",&y1,&m1,&d1) != EOF) { 
        //scanf ("%4d%2d%2d",&y2,&m2,&d2); 
        printf("%d\n",Abs(buf[y1][m1][d1]-buf[y1][1][1])+1);
    }
    return 0;
}
例2.4 打印日期 (九度教程第9题)
时间限制:1秒 **内存限制:32兆 ** 特殊判题:否

题目描述:
给出年分m和一年中的第n天,算出第n天是几月几号。

输入:
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出:
可能有多组测试数据,对于每组数据,按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

样例输入:
2000 3
2000 31
2000 40
2000 60
2000 61
2001 60

样例输出:
2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01

来源:
2003-2005年华中科技大学计算机研究生机试真题

#include 
#include
#include
#include
#include
using namespace std;
#define ISYEAP(x) x%100!=0&&x%4==0||x%400==0?1:0


int dayOfMonth[13][2]=
{
    {0,0},//0
    {31,31},//1
    {28,29},//2
    {31,31},//3
    {30,30},//4
    {31,31},//5
    {30,30},//6
    {31,31},//7
    {31,31},//8
    {30,30},//9
    {31,31},//10
    {30,30},//11
    {31,31}//12
};


int main()
{
    int y,n;
    while(scanf("%d %d",&y,&n)!=EOF)
    {
        int i;
        int m=ISYEAP(y);
        for(i=0;n>dayOfMonth[i][m];i++)
        {
            n-=dayOfMonth[i][m];
        }
        printf("%04d-%02d-%02d\n",y,i,n);
    }
}

你可能感兴趣的:(王道论坛计算机考研机试指南 二 日期类问题)