Stevehim @ 2022-10-03 22:20:36
RT
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
int check(string mon, string da) { //检查回文数
int month = stoi(mon), day = stoi(da);
// cout << month << ' ' << day << endl;
if (month == 0 || month > 12)
return 0;
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day > 31)
return 0;
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
return 0;
}
int year1, month1, day1;
int year2, month2, day2;
int sheng(string year4) {
string year3 = year4;
reverse(year3.begin(), year3.end());
string month, day;
month += year3[0];
month += year3[1];
day += year3[2];
day += year3[3];
// cout << "year3=" << year3 << ' ' << month << ' ' << day << endl;
if (!check(month, day)) {
return 0;
}
if (year4 == to_string(year1)) { //开判断
// cout << "ok" << endl;
if (stoi(month) >= month1 && stoi(day) >= day1) {
return 1;
} else {
return 0;
}
} else if (year4 == to_string(year2)) {
if (stoi(month) <= month2 && stoi(day) <= day2) {
return 1;
} else {
return 0;
}
} else {
return 1;
}
}
int sum = 0;
int main() {
scanf("%4d%2d%2d", &year1, &month1, &day1);
// cout << year1 << ' ' << month1 << ' ' << day1 << endl;
scanf("%4d%2d%2d", &year2, &month2, &day2);
// cout << year1 << ' ' << month1 << ' ' << day1 << endl;
//判断之间的年数,生成回文数
for (int i = year1; i <= year2; i++ ) {
if (sheng(to_string(i))) {
sum++;
}
}
cout << sum;
return 0;
}
by Wangyunzhe1688 @ 2022-10-07 20:31:07
#include<bits/stdc++.h>
using namespace std;
int f(int x){
int sum=0;
while(x){
sum=sum*10+x%10;
x/=10;
}return sum;
}
bool year(int y1){
if((y1%4 == 0&&y1%100 != 0)||y1%400 == 0){
return true;
} else {
return false;
}
}
bool month1(int m1){
if(m1>=1&&m1<=12) return true;
else return false;
}
bool day1(int m1,int y1,int d1){
if(m1 == 1||m1 ==3||m1 ==5||m1 ==7||m1 ==8||m1 ==10||m1 ==12) {
if(d1<=31&&d1>=1) return true;
else return false;
}
if(m1 ==4||m1 ==6||m1 ==11||m1 ==9){
if(d1<=30) return true;
else return false;
}
if(m1 == 2){
if(year(y1)){
if(d1<=29){
return true;
}else{
return false;
}
}else{
if(d1<=28){
return true;
}else{
return false;
}
}
}
}
int main(){
int y1,m1,d1,y2,m2,d2,cnt = 0;
scanf("%4d%2d%2d\n%4d%2d%2d",&y1,&m1,&d1,&y2,&m2,&d2);
for(int i = y1;i<=y2;i++){
int date = i*10000+f(i);
int month=date/100%100,day=date%100;
if(month1(month)&&day1(month,i,day)&& date >=y1*10000+m1*100+d1&&date<=y2*10000+m2*100+d2){
cnt++;
}
}
cout<<cnt;
return 0;
}
可以参考一下