枚举4.20
枚举 · 例3-回文日期
https://ac.nowcoder.com/acm/contest/20960/1006
/*知识点:字符串转整型stoi() 整型转字符串to_string() 取子串a.substr(起始位置,长度); reverse(s.begin(),s.end())反转字符串 整型二维数组给初值 思路:用字符串型输入,取子串年份,转整型,遍历年份,反转年份,取子串月和日,判断月,日是否合理(看题解用的二维数组,比较简便), 将遍历的每一个年月日转成整型,和日期上限比较(由于是从下限开始遍历的一定是大于下限的,自己写第一遍的时候把下限的年月日也提了出来,想要分别进行比较,没有想到一整个写成整型直接比较) 感受:这道题是听了课之后写的,知道枚举的思路是将年份反转,然后判断月日是否合理,但是写的时候很混乱,很纠结,由于上面转换的函数有些忘记了,有些不知道,导致选择数据类型时有点混乱,判 断是否合理这一部分只想着if,else讨论,乱的很,没想到用二维数组和整型直接比较。做题量少且思维不清晰。 */ #include <bits/stdc++.h> using namespace std; string a,b; int days[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}}; int main(){ cin>>a>>b; string y1=a.substr(0,4); string y2=b.substr(0,4); int ans=0; for(int i=stoi(y1);i<=stoi(y2);i++){ string s=to_string(i); // if(s.size()<4) s="0"+s; reverse(s.begin(),s.end()); string month=s.substr(0,2); string day=s.substr(2,2); int m=stoi(month); int d=stoi(day); int j=(i%4==0&&i%100!=0||i%400==0); if(m<1||m>12) continue; if(d>days[j][m]||d<1) continue; if(i*10000+m*100+d<=stoi(b)) ans++; } cout<<ans; return 0; }