OJ-Problems-Source/.ACM-Templates/TXTs/日期.txt

48 lines
1.6 KiB
Plaintext
Raw Normal View History

2016-11-22 09:38:35 +08:00
//<2F><><EFBFBD>ں<EFBFBD><DABA><EFBFBD>
char *week[] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Date { int y, m, d; };
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline int leap(int y) {
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}
//<2F>кϷ<D0BA><CFB7><EFBFBD>
inline bool legal(Date &a) {
if (a.m < 0 || a.m > 12) { return false; }
if (a.m == 2) { return a.d > 0 && a.d <= 28 + leap(a.y); }
return a.d > 0 && a.d <= days[a.m - 1];
}
//<2F>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD>ڴ<EFBFBD>С
inline int datecmp(Date &a, Date &b) {
if (a.y != b.y) { return a.y - b.y; }
if (a.m != b.m) { return a.m - b.m; }
return a.d - b.d;
}
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD>
int DaysOfTheWeek(Date a) {
if (a.m == 1 || a.m == 2) { a.m += 12; a.y--; } //1<><31>2<EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ǰһ<C7B0><D2BB><EFBFBD><EFBFBD>13, 14<31><34>
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>1752<35><32>9<EFBFBD><39>3<EFBFBD><33>֮ǰ
if (a.y < 1752 || (a.y == 1752 && a.m < 9) || (a.y == 1752 && a.m == 9 && a.d < 3)) {
return (a.d + 2 * a.m + 3 * (a.m + 1) / 5 + a.y + a.y / 4 + 5) % 7;
} else {
return (a.d + 2 * a.m + 3 * (a.m + 1) / 5 + a.y + a.y / 4 - a.y / 100 + a.y / 400) % 7;
}
}
//<2F><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB>
int date2int(Date a) {
int ret = a.y * 365 + (a.y - 1) / 4 - (a.y - 1) / 100 + (a.y - 1) / 400;
days[1] += leap(a.y);
for (int i = 0; i < a.m - 1; ret += days[i++]);
days[1] = 28;
return ret + a.d;
}
//<2F><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
Date int2date(int a) {
Date ret; ret.y = a / 146097 * 400;
for (a %= 146097; a >= 365 + leap(ret.y); a -= 365 + leap(ret.y), ret.y++);
days[1] += leap(ret.y);
for (ret.m = 1; a >= days[ret.m - 1]; a -= days[ret.m - 1], ret.m++);
days[1] = 28; ret.d = a + 1;
return ret;
}