⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1071.cpp

📁 这是哈尔滨工业大学acmOJ的源代码
💻 CPP
字号:
/*  This Code is Submitted by wywcgs for Problem 1071 on 2006-02-09 at 03:49:07 */ 
#include <cstdio>
#include <cstring>

const int LIMIT[] = { 1, 12, 0, 24, 60, 60 };
const int MONTH[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const char *DATE[] = { "year", "month", "day", "hour", "minute", "second" };
typedef long long int64;

inline bool leap(const int);
inline int leapN(const int);

class Date {
public:
	int n[6];
	bool make();
	int limit(int) const;
	void inc(int);
	void ceil(int);
};
int Date::limit(int o) const {
	int lmt = LIMIT[o];
	if(o == 2)
		if(leap(n[0]) && n[1] == 1) lmt = MONTH[1]+1;
		else lmt = MONTH[n[1]];
	return lmt;
}
bool Date::make() {
	int i;
	for(i = 0; i < 6; i++) 
		if(scanf("%d", &n[i]) == EOF) return false;
	n[6] = 0; n[1]--; n[2]--;
	return true;
}
void Date::inc(int o) {
	int i; n[o]++;
	for(i = o; i >= 0; i--)
		if(n[i] == limit(i)) n[i] = 0, n[i-1]++;
}
void Date::ceil(int o) {
	int i; bool up = false;
	for(i = o+1; i < 6; i++)
		if(n[i] != 0) up = true, n[i] = 0;
	if(up) inc(o);
}

const Date BEGIN = { 1970, 0, 0, 0, 0, 0 };

int64 from(const Date&, const Date&, int);

int main()
{
	Date begin, end;
	int T, po;
	char per[8];

	while(begin.make() && end.make()) {
		scanf("%d %s", &T, per);
		for(po = 0; strcmp(DATE[po], per); po++) ;
		begin.ceil(po);
		int64 r = from(BEGIN, end, po) - from(BEGIN, begin, po);
		printf("%lld\n", r/T);
	}
	
	return 0;
}

inline bool leap(const int y)
{
	return y%4 == 0 && !(y%100 == 0 && y%400 != 0);
}
inline int leapN(const int y)
{
	return y/4 - y/100 + y/400;
}
int64 from(const Date& b, const Date& e, int po)
{
	int64 t = e.n[0]-b.n[0], dn = 365*(e.n[0]-1-b.n[0])+leapN(e.n[0]-1)-leapN(b.n[0]);
	int i;
	if(po >= 1) t = t * 12 + e.n[1];
	if(po >= 2) {
		Date next = b; next.n[0] = e.n[0];
		for(i = 0; i < e.n[1]; i++) dn += next.limit(2), next.n[1]++;
		for(t = dn, i = 2; i <= po; i++) {
			if(i > 2) t *= LIMIT[i];
			t += e.n[i];
		}
	}
	
	return t;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -