检查数字在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 男孩上学祝福语简短的
18 入住酒店文案祝福语简短