You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.8 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
/*
string timeStr = "2017-05-27 19:50:02";
cout << timeStr << endl;
time_t timet = stringToDatetime(timeStr);
//5天后
timet += 5 * 24 * 3600;
string timeStr2 = datetimeToString(timet);
cout << timeStr2 << endl;
return 0;
*/
//字符串转日期
time_t stringToDatetime(string str) {
char *cha = (char *) str.data(); // 将string转换成char*。
tm tm_; // 定义tm结构体。
int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
sscanf(cha, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);// 将string存储的日期时间转换为int临时变量。
tm_.tm_year = year - 1900; // 年由于tm结构体存储的是从1900年开始的时间所以tm_year为int临时变量减去1900。
tm_.tm_mon = month - 1; // 月由于tm结构体的月份存储范围为0-11所以tm_mon为int临时变量减去1。
tm_.tm_mday = day; // 日。
tm_.tm_hour = hour; // 时。
tm_.tm_min = minute; // 分。
tm_.tm_sec = second; // 秒。
tm_.tm_isdst = 0; // 非夏令时。
time_t t_ = mktime(&tm_); // 将tm结构体转换成time_t格式。
return t_; // 返回值。
}
//日期转字符串
string datetimeToString(time_t time) {
tm *tm_ = localtime(&time); // 将time_t格式转换为tm结构体
int year, month, day, hour, minute, second;// 定义时间的各个int临时变量。
year = tm_->tm_year + 1900; // 临时变量由于tm结构体存储的是从1900年开始的时间所以临时变量int为tm_year加上1900。
month = tm_->tm_mon + 1; // 临时变量由于tm结构体的月份存储范围为0-11所以临时变量int为tm_mon加上1。
day = tm_->tm_mday; // 临时变量,日。
hour = tm_->tm_hour; // 临时变量,时。
minute = tm_->tm_min; // 临时变量,分。
second = tm_->tm_sec; // 临时变量,秒。
char yearStr[5], monthStr[3], dayStr[3], hourStr[3], minuteStr[3], secondStr[3];// 定义时间的各个char*变量。
sprintf(yearStr, "%d", year); // 年。
sprintf(monthStr, "%d", month); // 月。
sprintf(dayStr, "%d", day); // 日。
sprintf(hourStr, "%d", hour); // 时。
sprintf(minuteStr, "%d", minute); // 分。
if (minuteStr[1] == '\0') // 如果分为一位如5则需要转换字符串为两位如05。
{
minuteStr[2] = '\0';
minuteStr[1] = minuteStr[0];
minuteStr[0] = '0';
}
sprintf(secondStr, "%d", second); // 秒。
if (secondStr[1] == '\0') // 如果秒为一位如5则需要转换字符串为两位如05。
{
secondStr[2] = '\0';
secondStr[1] = secondStr[0];
secondStr[0] = '0';
}
char s[20]; // 定义总日期时间char*变量。
sprintf(s, "%s-%s-%s %s:%s:%s", yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr);// 将年月日时分秒合并。
string str(s); // 定义string变量并将总日期时间char*变量作为构造函数的参数传入。
return str; // 返回转换日期时间后的string变量。
}
//转换整数到字符串
string intToStr(int n) {
char str[256] = {0};
sprintf(str, "%d", n);
return str;
}
//字符串前补零
string fillZero(string old_string, int n_zero) {
string new_string = string(n_zero - old_string.length(), '0') + old_string;
return new_string;
}
//分割字符串
void split(const string &s, vector<string> &sv, const char flag = ' ') {
sv.clear();
istringstream iss(s);
string temp;
while (getline(iss, temp, flag)) {
//sv.push_back(stoi(temp)); //如果需要返回整数
sv.push_back(temp);
}
return;
}
//判断字符串是不是回文
bool isHuiWen(string s) {
string t = s;
reverse(t.begin(), t.end());
if (s == t) return true;
return false;
}
int main() {
int count = 0;
string date1, date2;
cin >> date1 >> date2;
//列举日期段内的所有日期
string year1, month1, day1, year2, month2, day2;
year1 = date1.substr(0, 4);
month1 = date1.substr(4, 2);
day1 = date1.substr(6, 2);
year2 = date2.substr(0, 4);
month2 = date2.substr(4, 2);
day2 = date2.substr(6, 2);
//拼接字符串
string s1 = year1 + '-' + month1 + '-' + day1 + " 00:00:00";
string s2 = year2 + '-' + month2 + '-' + day2 + " 00:00:00";
//开始日期
time_t startDate = stringToDatetime(s1);
time_t endDate = stringToDatetime(s2);
//循环
while (startDate <= endDate) {
//第一次切割
vector<string> sv;
split(datetimeToString(startDate), sv, ' ');
string s1 = sv[0];
//第二次切割
sv.clear();
split(s1, sv, '-');
//拼接的结果
string result = sv[0] + fillZero(sv[1], 2) +
fillZero(sv[2], 2);
if (isHuiWen(result)) {
//cout << result << endl;
count++;
}
//增加一天
startDate += 1 * 24 * 3600;
}
//输出
cout << count << endl;
return 0;
}