日期问题本地打表做法
作者:
无苦邪
,
2024-04-03 18:22:54
,
所有人可见
,
阅读 5
#include<bits/stdc++.h>
using namespace std;
int days[13] = {0,31,28,31,30,31,30,31,31,31,30,31,30};
bool check_valid(int x) {
string s = to_string(x);
string temp = s;
reverse(temp.begin(),temp.end());
return s == temp;
}
bool check_data(int x) {
int year = x / 10000;
int month = x % 10000 /100;
int day = x % 10000 % 100;
if(day == 0) return false;
if(!day|| ! month|| month >= 13) return false;
int is_run = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
if(is_run) {
days[2] = 29;
} else {
days[2] = 28;
}
if(days[month] < day) return false;
return true;
}
=========== 以数组的方式把符合条件的日期以days[i] 方式写入文件 =====
void print_data() {
freopen("F:\\data.txt","w",stdout);
ifstream file("F:\\out.txt");
vector<string> days;
string line;
if (file.is_open()) {
while (getline(file, line)) {
days.push_back(line);
}
file.close();
}
for(int i = 0;i < days.size();i ++) {
cout << "days[" << i << "] = " << days[i] << endl;
}
fclose(stdout);
}
int main() {
print_data();
}
=============== 将符合条件的日期写入文件 ===========