思路
此题对于首中尾相等数的判断用整数来写会有点麻烦,因此我们考虑将整数转化为字符串来写。
介绍一个很好用的函数 to_string(x),它能够将整型转化为字符串。于是就很简单了,我们只需判断字符串长度是否为奇数以及字符串首位末位和中间位是否相等即可。
AC code:
#include <bits/stdc++.h>
using namespace std;
int T,l,r;
bool check(string s){
if(s.size()%2&&s[0]==s[s.size()-1]&&s[0]==s[s.size()/2]) return 1;
return 0;
}
int main(){
cin>>T;
while(T--){
cin>>l>>r;
for(int i = l;i<=r;i++) if(check(to_string(i))) cout<<i<<' ';
cout<<'\n';
}
return 0;
}
暂无评论