题解:P11680 [Algo Beat Contest 001 B] Between Head and Tail

· · 题解

难点在于把数字转化成字符串。

这里使用 to_string 函数,可以提供一个数字给它,他会返回一个字符串。所以可以直接把数字转化成字符串,剩下直接按下标来处理和模拟就好了。

代码:


#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,l,r;
bool check(string s){
    if(s.size()%2==1&&s[0]==s[s.size()-1]&&s[0]==s[s.size()/2]){
        return true;
    }
    return false;
}
signed main(){
    cin>>t;
    while(t--){
        cin>>l>>r;
        for(int i=l;i<=r;i++){
            string h=to_string(i);
            if(check(h)) cout<<h<<" ";
        }
        cout<<endl;
    }
    return 0;
}