20231224
This commit is contained in:
185
ACMOJ-1073.cpp
Normal file
185
ACMOJ-1073.cpp
Normal file
@ -0,0 +1,185 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
class Date {
|
||||
private:
|
||||
int _year;
|
||||
int _month;
|
||||
int _day;
|
||||
|
||||
static bool IsValid(int year, int month, int day) {
|
||||
if (month < 1 || month > 12) return false;
|
||||
if (month != 2) return 1 <= day && day <= month_days[month];
|
||||
return 1 <= day && day <= month_days[month] + IsLeapYear(year);
|
||||
}
|
||||
static Date *bucket;
|
||||
static int bucket_cnt, it_cnt;
|
||||
static int GetId(const Date &dt) {
|
||||
if (bucket == nullptr) {
|
||||
bucket = new Date[49275];
|
||||
for (int year = 1900; year <= 2030; year++)
|
||||
for (int month = 1; month <= 12; month++) {
|
||||
int ds = month_days[month];
|
||||
if (month == 2 && IsLeapYear(year)) ds++;
|
||||
for (int day = 1; day <= ds; day++) {
|
||||
bucket[bucket_cnt++] = Date(year, month, day);
|
||||
}
|
||||
}
|
||||
}
|
||||
int L = 0, R = bucket_cnt, M;
|
||||
while (L < R) {
|
||||
M = (L + R) >> 1;
|
||||
if (bucket[M] < dt)
|
||||
L = M + 1;
|
||||
else if (dt < bucket[M])
|
||||
R = M;
|
||||
else
|
||||
return M;
|
||||
}
|
||||
return (L + R) >> 1;
|
||||
}
|
||||
|
||||
public:
|
||||
// 构造函数
|
||||
Date() : _year(0), _month(0), _day(0) {
|
||||
it_cnt++;
|
||||
_year = 1900;
|
||||
_month = 1;
|
||||
_day = 1;
|
||||
}
|
||||
Date(int yy, int mm, int dd) {
|
||||
it_cnt++;
|
||||
if (!IsValid(yy, mm, dd)) {
|
||||
_year = 1900;
|
||||
_month = 1;
|
||||
_day = 1;
|
||||
return;
|
||||
}
|
||||
_year = yy;
|
||||
_month = mm;
|
||||
_day = dd;
|
||||
}
|
||||
~Date() {
|
||||
it_cnt--;
|
||||
if (it_cnt == 0) {
|
||||
delete[] bucket;
|
||||
bucket = nullptr;
|
||||
bucket_cnt = 0;
|
||||
}
|
||||
}
|
||||
// 判断是否为闰年
|
||||
static bool IsLeapYear(int year) {
|
||||
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 一个日期加上一个天数
|
||||
Date operator+(int det) {
|
||||
int id = GetId(*this);
|
||||
return bucket[id + det];
|
||||
}
|
||||
// 一个日期减去一个天数
|
||||
Date operator-(int det) {
|
||||
int id = GetId(*this);
|
||||
return bucket[id - det];
|
||||
}
|
||||
// 前置++
|
||||
Date &operator++() {
|
||||
*this = *this + 1;
|
||||
return *this;
|
||||
}
|
||||
// 后置++
|
||||
Date operator++(int) {
|
||||
Date tmp = *this;
|
||||
*this = *this + 1;
|
||||
return tmp;
|
||||
}
|
||||
// 前置--
|
||||
Date &operator--() {
|
||||
*this = *this - 1;
|
||||
return *this;
|
||||
}
|
||||
// 后置--
|
||||
Date operator--(int) {
|
||||
Date tmp = *this;
|
||||
*this = *this - 1;
|
||||
return tmp;
|
||||
}
|
||||
int operator-(const Date &B) {
|
||||
int ida = GetId(*this);
|
||||
int idb = GetId(B);
|
||||
return ida > idb ? ida - idb : idb - ida;
|
||||
}
|
||||
//<重载
|
||||
bool operator<(const Date &B) const {
|
||||
if (_year != B._year) return _year < B._year;
|
||||
if (_month != B._month) return _month < B._month;
|
||||
return _day < B._day;
|
||||
}
|
||||
// 重载输出运算符
|
||||
friend ostream &operator<<(ostream &stream, const Date &dt);
|
||||
// 以上仅为提示,不代表你需要完成所有,你也可以添加其他需要的函数。
|
||||
};
|
||||
Date *Date::bucket = nullptr;
|
||||
int Date::it_cnt = 0;
|
||||
int Date::bucket_cnt = 0;
|
||||
ostream &operator<<(ostream &stream, const Date &dt) {
|
||||
stream << dt._year << '-' << dt._month << '-' << dt._day;
|
||||
return stream;
|
||||
}
|
||||
|
||||
void Test() {
|
||||
int op;
|
||||
cin >> op;
|
||||
int yy, mm, dd;
|
||||
if (op == 1 || op == 0) {
|
||||
Date d0;
|
||||
Date d1(2000, 2, 29);
|
||||
Date d2(1900, 2, 29);
|
||||
cout << d0 << endl;
|
||||
cout << d1 << endl;
|
||||
cout << d2 << endl;
|
||||
// d0.out(); d1.out(); d2.out();
|
||||
}
|
||||
if (op == 2 || op == 0) {
|
||||
cin >> yy >> mm >> dd;
|
||||
Date d0(yy, mm, dd);
|
||||
for (int i = 0; i < 5; ++i) cout << ++d0 << endl; //(++d0).out();
|
||||
for (int i = 0; i < 5; ++i) cout << d0++ << endl; //(d0++).out();
|
||||
for (int i = 0; i < 5; ++i) cout << d0-- << endl; //(d0--).out();
|
||||
for (int i = 0; i < 2; ++i) cout << --d0 << endl; //(--d0).out();
|
||||
cout << d0 << endl;
|
||||
// d0.out();
|
||||
}
|
||||
if (op == 3 || op == 0) {
|
||||
cin >> yy >> mm >> dd;
|
||||
Date d0(yy, mm, dd);
|
||||
cout << d0 + 100 << endl;
|
||||
// (d0+100).out();
|
||||
cout << d0 - 1000 << endl;
|
||||
// (d0-1000).out();
|
||||
}
|
||||
if (op == 4 || op == 0) {
|
||||
cin >> yy >> mm >> dd;
|
||||
Date d0(yy, mm, dd);
|
||||
Date d1(2020, 12, 21);
|
||||
cout << (d0 < d1) << endl;
|
||||
}
|
||||
if (op == 5 || op == 0) {
|
||||
cin >> yy >> mm >> dd;
|
||||
Date d0(yy, mm, dd);
|
||||
Date d1(1912, 6, 23);
|
||||
cout << d0 - d1 << endl;
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
#ifdef local
|
||||
freopen("pro.in", "r", stdin);
|
||||
#endif
|
||||
Test();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user