题目传送门
铺垫
题目就是让我们在 L 到 R 之间找到一个首中尾相等数喵
『首中尾相等数的定义是什么呢 qwq』
先假设我们现在判断的这个数为 x
那么 x 应满足以下条件:
- x 有奇数个数位
- x 的首位、末位和最中间的数位上的数字相等
知道了这些,我们就可以去实现了 awa
实现
第一步:找到我们要进行判断的数
这一步相信大家都知道:用
for循环从 L 到 R 依次枚举就可以了喵C++ for (int i=l; i<=r; i++)
第二步:对这个数进行判断
这一步可以写一个函数,来判断其是否满足条件
函数内步骤:
- 判断数位(偶数位返回
false, 一位返回true)C++ int xx=x,cnt=0;//用cnt来算这个数的位数 while(xx) { cnt++; xx/=10; } if(cnt%2==0)//偶数位,不行 return false; if(cnt==1)//特判一位数 return true;
- 提取出首位、中位以及末位
C++ int shou=x/pow(10,cnt-1);//首位 int zhong=x/pow(10,cnt/2);//中位 zhong%=10;//只有一位,记得取余 int wei=x%10;//末位
- 判断是否满足条件
C++ return (shou==zhong && shou==wei);
第三步:输出
如果满足条件就直接输出
完整代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, l, r;
bool check (int x)
{
int xx = x, cnt = 0;//用cnt来算这个数的位数
while (xx)
{
cnt ++;
xx /= 10;
}
if (cnt % 2 == 0)//偶数位,不行
return false;
if (cnt == 1)//特判一位数
return true;
int shou = x / pow(10,cnt-1);//首位
int zhong = x / pow(10,cnt/2);//中位
zhong %= 10;//只有一位,记得取余
int wei = x % 10;//末位
return (shou == zhong && shou == wei);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while (t--)
{
cin >> l >> r;//输入左端点和右端点
//因为这题比较水,所以暴力能过qwq
for (int i = l; i <= r; i++)//从l到r依次枚举
{
if(check(i))//满足条件
cout << i << " ";
}
cout << "\n";
}
return 0;
}
记得每一组数据后输出换行哦 qwq
喵
好啦,到这的话这道题就已经结束了,有什么问题欢迎来问我喵~o (=∩ω∩=) m
再附一个 AC 记录 ~(拜拜