检查数字在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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短