检查数字在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 婆婆高寿祝福语大全简短