对于两个整数a,b,如果b是严格大于a且各位数字均不相同的最小整数,就视b为a的跟屁虫。现在给定a,请用C++语言找出它的跟屁虫b。1000≤a≤9000》
时间: 2023-12-27 12:05:34 浏览: 29
以下是C++代码实现:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a;
cin >> a;
while (true) {
a++;
int b = a;
bool digits[10] = {false}; // 标记数字是否出现过
bool flag = true; // 是否满足条件的标记
while (b) {
int digit = b % 10;
if (digits[digit]) {
flag = false;
break;
}
digits[digit] = true;
b /= 10;
}
if (flag) {
cout << a << endl;
break;
}
}
return 0;
}
```
首先读入整数a,然后从a+1开始逐个枚举整数b,判断b是否满足各位数字不相同的条件。如果满足条件,则输出b并退出循环。
相关问题
跟屁虫 对于两个整数a,b,如果b是严格大于a且各位数字均不相同的最小整数,就视b为a的跟屁虫。c++
实现方式如下:
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int a;
cin >> a;
while (true) {
++a;
string s = to_string(a);
sort(s.begin(), s.end());
if (unique(s.begin(), s.end()) == s.end()) {
cout << a << endl;
break;
}
}
return 0;
}
```
首先输入整数a,然后从a+1开始循环,每次将当前数转换成字符串并排序,判断排序后的字符串是否有重复字符,如果没有则输出当前数并结束循环。
对于两个整数a,b,如果b是严格大于a且各位数字均不相同的最小整数,就视b为a的跟屁虫。现在给定a,请正在学习编程的你找出它的跟屁虫b。
可以按照以下步骤来找到a的跟屁虫b:
1. 令n = a + 1。
2. 当n不是严格大于a时,将n加1。
3. 将n转换为字符串,判断其中的数字是否有重复。
4. 如果n中的数字没有重复,则n是a的跟屁虫。如果有重复,则返回步骤2。
下面是一个Python实现的示例代码:
```python
def find_follower(a):
n = a + 1
while n <= a or len(set(str(n))) != len(str(n)):
n += 1
return n
```
可以用以下代码测试:
```python
print(find_follower(123)) # 输出124
print(find_follower(1987)) # 输出2013
```
注意,这个算法并不能保证一定能找到a的跟屁虫,因为可能不存在满足条件的整数b。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)