정수가 있다고 가정하고 숫자가 자릿수의 계승의 합을 나누는지 찾아야 합니다. 숫자가 19이고 계승의 합이 (1! + 9!) =362881이라고 가정하고 이것은 19로 나눌 수 있습니다.
이 문제를 해결하기 위해 숫자를 취한 다음 각 자릿수의 계승을 계산하고 합을 더합니다. 합이 숫자 자체로 나눌 수 있으면 true를 반환하고 그렇지 않으면 false를 반환합니다.
예시
#include <iostream>
using namespace std;
int factorial(int n){
if(n == 1 || n == 0)
return 1;
return factorial(n - 1) * n;
}
bool isDigitsFactDivByNumber(int num){
int temp = num;
int sum = 0;
while(num){
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}if(sum%temp == 0){
return true;
} return false;
}
int main() {
int number = 19;
if (isDigitsFactDivByNumber(number))
cout << "Yes, the number can divides the sum of factorial of digits.";
else
cout << "No, the number can not divides the sum of factorial of digits.";
} 출력
Yes, the number can divides the sum of factorial of digits.