递归检查一个数字的所有数字是否都不相同
Recursively check if all digits of a number are different
如何在c++中递归地检查整数的所有数字是否为不同的数字
void Check(int n)
{
if (n < 10)
return;
else
{
bool eq = !(n % 10 == ((n / 10) % 10));
if (eq == true)
{
Check(n / 10);
}
}
}
你可以记住,哪些数字你已经看到了。例如,使用长度为10的bool数组。在第一次调用函数时,所有的项都是false
。在每个递归调用中,将array[n%10]
设置为true。如果它已经是true
,那么你已经找到了一个重复的数字,否则没有。
如果只想使用递归,可以定义第二个递归函数:
bool checkIfDigitApearsInNumber(int n, int digit) {
if (n == 0) {
return false;
} else {
if (n % 10 == digit) {
return true;
} else {
return checkIfDigitApearsInNumber(n/10, digit);
}
}
}
在函数Check
中,您必须在n/10, n%10
的每个步骤中调用该函数。
相关文章:
- 用于检查数字是否有数字或可被该数字的值整除的程序
- 如何使用递归检查数字是否有重复数字?
- 查找数字是否为 2 的幂的时间复杂度
- 将 C 函数转换为 C++ 以检查数字是否有效
- 检查数字是否为素数的算法
- 在C/C++中检查数字是否为整数(不带scanf/gets/etc)
- 如何以更有效的方式检查一个数字是否是素数?
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- 如何检查字段中输入的数字是否重复
- 查看数字是否包含在未排序的双数字间隔内的最有效方法?
- 检查数字是否已在数组中的程序
- 编写一个函数,用递归函数检查数字是否是正方形
- 使用模量来找出数字是否可以除去不起作用的变量
- 查找数字是否为素数 c++
- 如何测试某些数字是否沿区间均匀分布?
- 这是确定两个数字是否具有相同奇偶校验的正确方法
- 如何检查 stdin 中的数字是否小于给定类型的数字限制
- 更好的算法来检查一个数字是否既不是素数也不是单个素数的幂
- 如何在不使用数学的情况下知道一个数字是否是回文
- 验证每个数字是否在特定条件下