logo Algo Beat Contest
登录 注册

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

作者: YANGRONGYANG  ·  发布于 2026-05-12 19:01:26  ·  最后修改于 2026-05-12 19:49:16
已通过

思路

此题对于首中尾相等数的判断用整数来写会有点麻烦,因此我们考虑将整数转化为字符串来写。

介绍一个很好用的函数 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;
}

暂无评论

登录 后即可评论。