算法笔记 —— 3.4 日期处理


1. Problem A 日期差值

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。

闰年与非闰年的月份有差别,可以存在如下的二维数组中。

#include<iostream>
#include <stdio.h>
using namespace std;

bool isLeap(int year);

int month[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}
};

int main() {
	int t1, t2, y1, m1, d1, y2, m2, d2;

	while (scanf("%d %d", &t1, &t2) != EOF) {

		int res = 1;

		//令t1大于t2
		if (t1 < t2) {
			int temp = t1;
			t1 = t2;
			t2 = temp;
		}

		y1 = t1 / 10000; m1 = (t1 % 10000) / 100; d1 = t1 % 100;
		y2 = t2 / 10000; m2 = (t2 % 10000) / 100; d2 = t2 % 100;

		while (y1 > y2 || m1 > m2 || d1 > d2) {
			d2++;
			if (d2 == month[m2][isLeap(y2)] + 1) {
				m2++;
				d2 = 1;
			}
			if (m2 == 13) {
				y2++;
				m2 = 1;
			}
			res++;
		}
		cout << res << endl;
	}
}

//判断是否为闰年
bool isLeap(int year) {
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		return true;
	else
		return false;
}

2. Problem 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.

您的任务是编写一个程序,该程序将使用今天有关约会的协议来计算与最近的日期或将来的给定日期相对应的星期几。

在给定日期前和给定日期后的操作有所不同,注意判别

#pragma warning(disable:4996)
#include <iostream>
#include <stdio.h>
#include <algorithm> 
#include <string.h>
using namespace std; 


int month[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 montename[13][10] = { 
	" ",
	"January","February","March","April",
	"May","June","July","August","September",
	"October","November","December" 
};

char weekname[8][10] = { 
	" ",
	"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" 
};

int ansnumber(int y0,int m0,int d0,int y, int m, int d) {
	int res = 0;
	while (y > y0 || m > m0 || d > d0) {
		d0++;
		if (d0 == month[m0][(y0 % 4 == 0 && y0 % 100 != 0) || (y0 % 400 == 0)] + 1) {
			m0++;
			d0 = 1;
		}
		if (m0 == 13) {
			y0++;
			m0 = 1;
		}
		res++;
	}
	return res;
}

int findmonth(char mn[]) {
	int i;
	for (i = 1; i < 13; i++) {
		if (strcmp(mn, montename[i]) == 0)
			break;
	}
	return i;

}

int main() {
	int y, m, d, w, ans;
	char mn[10];
	//2020年5月1日 星期五
	while (scanf("%d %s %d", &d, &mn, &y )!= EOF) {
		m = findmonth(mn);
		int y0 = 2020, m0 = 5, d0 = 3, w0 = 7;
		if (y0 > y || (y0 == y && m0 > m) || (y0 == y && m0 == m && d0 > d)) {
			swap(y, y0); swap(m, m0); swap(d, d0);
			ans = ansnumber(y0, m0, d0, y, m, d);
			w = w0 - ans % 7;
		}
		else {
			ans = ansnumber(y0, m0, d0, y, m, d);
			w = w0 - 7 + ans % 7;
		}
		cout << weekname[w] << endl;
	}
}

3. Problem C 打印日期

给出年分m和一年中的第n天,算出第n天是几月几号。

由于输入范围最大为366,闰年只有365天,可能会产生“非闰年” 366 的测试用例,应在最后一步判断是否超出一天,使得月份变为13

#pragma warning(disable:4996)
#include <iostream>
#include <stdio.h>
#include <algorithm> 
#include <string.h>
using namespace std; 


int month[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}
};


int main() {
	int y, m, d;
	while (scanf("%d %d", &y, &d )!= EOF) {
		m = 1;                            
		for (int i = 1; i <= 12; i++) {
			if (d > month[m][(y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)]) {
				d = d - month[m][(y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)];
				m++;
			}
			else break;
		}
		if (m > 12) {
			m = 1;
			y++;
		}
		printf("%.4d-%.2d-%.2d\n", y, m, d);
	}
}

4. Problem D 日期类

编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。测试数据不会有闰年。

简单

#pragma warning(disable:4996)
#include <iostream>
#include <stdio.h>
#include <algorithm> 
#include <string.h>
using namespace std;


int month[13] = {
		0,
		31,28,31,30,
		31,30,31,31,
		30,31,30,31
};


int main() {
	int n, y, m, d;
	scanf("%d", &n);
	for (int i = n; i > 0; i--) {
		scanf("%d %d %d", &y, &m, &d);
		d += 1;
		if (d > month[m]) {
			m++; d = 1;
		}
		if (m > 12) {
			y++; m = 1;
		}
		printf("%.4d-%.2d-%.2d\n", y, m, d);
	}
}

5. Problem E 日期累加

设计一个程序能计算一个日期加上若干天后是什么日期。

简单

#pragma warning(disable:4996)
#include <iostream>
#include <stdio.h>
#include <algorithm> 
#include <string.h>
using namespace std; 

int month[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} 
};



int main() {
	int num, n, y, m, d;
	scanf("%d", &num);
	for (int i = num; i > 0; i--) {
		scanf("%d %d %d %d", &y, &m, &d, &n);
		for (int j = n; j > 0; j--) {
			d += 1;
			if (d > month[m][(y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)]) {
				m++; d = 1;
			}
			if (m > 12) {
				y++; m = 1;
			}
		}
		
		printf("%.4d-%.2d-%.2d\n", y, m, d);
	}
}
posted @ 2020-05-01 22:51  马尔尤尤  阅读(388)  评论(0编辑  收藏  举报