题解:P11680 [Algo Beat Contest 001 B] Between Head and Tail
题解:P11680 [Algo Beat Contest 001 B] Between Head and Tail
题目传送门
挺好的模拟题。
题目思路
首先,我们要找到的数一定要满足以下条件:
- \(X\) 有奇数个数位。
- \(X\) 的首位、末位和最中间的数位上的数字相等。
所以我们可以统计这个数的位数,并将它的每一位数都存进一个数组 \(a\) 中。
这里的存储是倒着存的,因为不论这个数组是正序还是倒序,首尾和中间项都不变。
我们显然可以发现这个数 \(X\) 的第一位即为 \(a_{n}\),最后一位即为 \(a_1\),而中间一位即为 \(a_{\frac{n+1}{2}}\)。
最后枚举每个区间 \([L,R]\),求出即可。注意对于输出空格的判断。
代码实现
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool check(int a){
int sum=0,ans[1001],i=1,temp,temp1,temp2,b;
while(a>0){
b=a%10,sum++,ans[i]=b,i++,a=a/10;
}
temp=ans[1],temp1=ans[sum],temp2=ans[(sum+1)/2];
if(temp==temp1&&temp1==temp2&&temp==temp2&&sum%2!=0) return 1;
else return 0;
}
int main(){
int T,l,r;
cin>>T;
while(T--){
cin>>l>>r;
int ans1=0;
for(int i=l;i<=r;i++){
if(check(i)==1) cout<<i<<" ",ans1++;
}
if(ans1==0) cout<<"\n";
else cout<<"\n";
}
return 0;
}