二日期類問題
例2.3 | 日期差值 | (九度教程第6題) |
---|---|---|
時間限制:1秒 | **內(nèi)存限制:32兆 ** | 特殊判題:否 |
題目描述:
有兩個日期渠旁,求兩個日期之間的天數(shù)剂跟,如果兩個日期是連續(xù)的我們規(guī)定他們之間的天數(shù)為兩天
輸入:
有多組數(shù)據(jù)沼本,每組數(shù)據(jù)有兩行敬察,分別表示兩個日期漠畜,形式為YYYYMMDD
輸出:
每組數(shù)據(jù)輸出一行币他,即日期差值
樣例輸入:
20110412
20110422
樣例輸出:
11
來源:
2009年上海交通大學(xué)計算機研究生機試真題
#include <stdio.h>
#define ISYEAP(x) x%100 != 0 && x%4 ==0 || x%400 == 0 ? 1:0
// 定義宏判斷是否是閏年,方便計算每月天數(shù)
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
}; // 預(yù)存每天的天數(shù)憔狞,注意二月配合宏定義做特殊處理
struct Date {
int Day;
int Month;
int Year;
void nextDay() {
Day ++;
if (Day > dayOfMonth[Month][ISYEAP(Year)]) { //若天數(shù)超過了當月最大日數(shù)
Day = 1;
Month ++; // 進入下一月
if(Month > 12) { //月數(shù)超過12
Month = 1;
Year ++; // 進入下一年
}
}
}
};
int buf[5001][13][32]; //保存預(yù)處理的天數(shù)
int Abs(int x) { // 求絕對值
return x < 0 ? -x: x;
}
int main () {
Date tmp;
int cnt = 0; //天數(shù)計數(shù)
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日的天數(shù)差保存起來
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);//用預(yù)處理的數(shù)據(jù)計算兩日期差值,注意需對其求絕對值
}
return 0;
}
例2.4 | Day of week | (九度教程第7題) |
---|---|---|
時間限制:1秒 | **內(nèi)存限制: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 年上海交通大學(xué)計算機研究生機試真題
#include <stdio.h>
#include <string.h>
#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"
};//月名 每個月名對應(yīng)下標1到12
char weekName[7][20] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};//周名 每個周名對應(yīng)下標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 ++;
}//以上與上題一致,預(yù)處理出每一天與原點日期的天差數(shù)
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;//將輸入字符串與月名比較得出月數(shù)
}
}
int days = buf[y][m][d]-buf[2012][7][16];//計算給定日期與今日日期的天數(shù)間隔(注意可能為負)
days += 1;//今天(2012.7躯喇。16)為星期一辫封,對應(yīng)數(shù)組下標為1,則計算1進經(jīng)過days天后的下標
puts(weekName[(days%7+7)%7]);//將計算后得出的下標用7對其取模廉丽,并且保證其為非負數(shù)倦微,則該下標極即為答案所對應(yīng)的下標,輸出即可
}
return 0;
}
例2.4 | 今年的第幾天正压? | (九度教程第8題) |
---|---|---|
時間限制:1秒 | **內(nèi)存限制:32兆 ** | 特殊判題:否 |
題目描述:
輸入年欣福、月、日焦履,計算該天是本年的第幾天拓劝。
輸入:
包括三個整數(shù)年(1<=Y<=3000)、月(1<=M<=12)嘉裤、日(1<=D<=31)郑临。
輸出:
輸入可能有多組測試數(shù)據(jù),對于每一組測試數(shù)據(jù)屑宠,
輸出一個整數(shù)厢洞,代表Input中的年、月典奉、日對應(yīng)本年的第幾天躺翻。
樣例輸入:
1990 9 202000 5 1
樣例輸出:
263122
來源:
2003年清華大學(xué)計算機研究生機試真題
#include <stdio.h>
#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秒 | **內(nèi)存限制:32兆 ** | 特殊判題:否 |
題目描述:
給出年分m和一年中的第n天,算出第n天是幾月幾號卫玖。
輸入:
輸入包括兩個整數(shù)y(1<=y<=3000)公你,n(1<=n<=366)。
輸出:
可能有多組測試數(shù)據(jù)假瞬,對于每組數(shù)據(jù)陕靠,按 yyyy-mm-dd的格式將輸入中對應(yīng)的日期打印出來嚣崭。
樣例輸入:
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年華中科技大學(xué)計算機研究生機試真題
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
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);
}
}