2 solutions
-
0
字符串判断回文(60pts)
- 60%的数据只有一个数据,可以通过利用字符串的翻转判断是否是回文
#include<bits/stdc++.h> using namespace std; int main() { string s1,s2; cin>>s1>>s2; if(s1==s2) //只有一个日期 { string s=s1; reverse(s.begin(),s.end()); //翻转串 if(s==s1) //是回文 { cout<<1; } else //不是回文 { cout<<0; } } return 0; }
枚举年份(100pts)
- 由于结果是回文,那么我们可以通过年份构造对应的回文日期,那么我们只需要判断当前构造出来的日期是否合法即可。
#include<bits/stdc++.h> using namespace std; int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; bool check(int date) //判断日期是否合法 { int year=date/10000,month=(date%10000)/100,day=date%100; if(month==0||day==0||month>12) return false; //年份和月份不满足要求 bool isleapyear=year%400==0||year%4==0&&year%100!=0; //闰年判断 if(month!=2) //非二月的判断 { if(months[month]<day) return false; } else //二月的判断 { if(months[month]+isleapyear<day) return false; } return true; } int main() { int l,r; int cnt=0; cin>>l>>r; for(int i=1000;i<=10000;i++) //枚举所有的四位数 { int t=i; int res=0; //翻转年份的结果 while(t) { res=res*10+t%10; t/=10; } t=i*10000+res; //构造出来的日期 if(t<l||t>r) continue; //范围之外 if(check(t)) cnt++; //满足要求 } cout<<cnt; return 0; }
- 1
Information
- ID
- 1052
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 10
- Tags
- # Submissions
- 6
- Accepted
- 3
- Uploaded By