숫자 n이 있고 3으로 나눌 수 있지만 6으로 나눌 수 없는 이 수의 순열을 찾아야 한다고 가정합니다. 그러한 값을 만들 수 없으면 -1을 반환합니다. 예를 들어, n이 336이면 출력은 363이 될 수 있습니다.
우리가 알고 있듯이 숫자는 6으로 나눌 수 있다는 것은 3과 2로 나눌 수 있음을 의미합니다. 따라서 3으로 나눌 수 있는 각 짝수는 6으로 나눌 수 있습니다. , 이상하게도 결과가 됩니다.
예
#include<iostream>
#include<cmath>
using namespace std;
int findNumber(int n) {
int digit_count = ceil(log10(n));
for (int i = 0; i < digit_count; i++) {
if (n % 2 != 0) {
return n;
} else {
n = (n / 10) + (n % 10) * pow(10, digit_count - i - 1);
continue;
}
}
return -1;
}
int main() {
int n = 132;
cout <<"The permutation of "<<n << " that is divisible by 3 but not by 6 is:"<< findNumber(n);
} 출력
The permutation of 132 that is divisible by 3 but not by 6 is:213