여기에서 숫자가 뒤죽박죽인지 여부를 확인하는 흥미로운 문제가 하나 있습니다. 모든 숫자에 대해 인접 숫자가 최대 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