检查数字在C ++中是否混乱
在这里,我们将看到一个有趣的问题,检查数字是否混乱。如果每个数字的邻居数字相差最大1,则该数字被认为是混杂的。例如,数字1223被混杂,但1256未被混杂。
为了解决这个问题,我们必须检查一个数字是否具有相差大于1的邻居。如果找到该数字,则返回false,否则返回true。
示例
#include <iostream>
#include <cmath>
using namespace std;
bool isJumbled(int number) {
if (number / 10 == 0) //for single digit number is is always jumbled
return true;
while (number != 0) {
if (number / 10 == 0) //when all digits have checked, return true
return true;
int curr_digit = number % 10;
int prev_digit = (number / 10) % 10;
if (abs(prev_digit - curr_digit) > 1)
return false;
number = number / 10;
}
return true;
}
int main() {
int n = 1223;
if(isJumbled(n)){
cout << n << " is Jumbled";
} else {
cout << n << " is not Jumbled";
}
}输出结果
1223 is Jumbled
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短