MarsCode--完美整数【简单】
题目描述
问题描述
一个整数如果由相同数字构成,可以称为完美整数;比如说1、11、333就是完美整数,12、19、101就是不完美的整数。
现在想知道,在区间 [x, y]
中有多少个整数是完美整数。
输入格式
每个样例有一行,是整数 x
和 y
;(1 ≤ x ≤ y ≤ 10^9)
输出格式
每一个样例一行,是整数 m
,表示区间 [x, y]
中有 m 个整数是完美整数。
输入样例1
1 10
输出样例1
9
输入样例2
2 22
输出样例2
10
数据范围
1 ≤ t ≤ 1000
1 ≤ x ≤ y ≤ 10^9
算法分析
数字类的问题处理可以用%10,/10处理
完整代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool fun(long i)
{
//获取最低位置
int x=i%10;
while(i>0)
{
int cur=i%10;
if(cur!=x)//如果当前位置不等于最低位,返回false
return false;
i/=10;
}
return true;
}
int solution(int x, int y) {
// Edit your code here
int count=0;
for(long i=x;i<=y;i++)
{
if(fun(i))
count++;
}
return count;
return -1;
}
int main() {
// Add your test cases here
std::cout << (solution(1, 10) == 9) << std::endl;
std::cout << (solution(2, 22) == 10) << std::endl;
return 0;
}
原文地址:https://blog.csdn.net/m0_75266675/article/details/142897059
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!