이 문제에서 우리는 숫자 N이 주어집니다. 우리의 임무는 숫자의 모든 소수점을 출력하는 것이고, 그렇지 않으면 소수점이 없다면 -1을 출력하는 것입니다.
프라임 포인트 숫자를 두 개의 소수로 나누는 인덱스 값입니다. 하나는 왼쪽에, 다른 하나는 오른쪽에 있습니다.
문제를 이해하기 위해 예를 들어보겠습니다.
Input: 2359 Output: 1
설명 :인덱스 1에서 숫자를 나눌 때. 2와 59를 두 개의 소수로 얻습니다.
이 문제를 해결하기 위해 숫자에 대해 좌우 나누기가 가능한지 확인합니다. 유효한 경우 생성할 수 있는 모든 숫자 조합을 시도하고 소수인지 여부를 확인합니다. 소수인 경우 인덱스를 인쇄하십시오.
아래 코드는 솔루션 구현을 보여줍니다.
예시
#include <bits/stdc++.h> using namespace std; int countDigits(int n) { int count = 0; while (n > 0){ count++; n = n/10; } return count; } int checkPrime(int n) { if (n <= 1) return -1; if (n <= 3) return 0; if (n%2 == 0 || n%3 == 0) return -1; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return -1; return 0; } void primePoints(int n) { int count = countDigits(n); if (count==1 || count==2){ cout << "-1"; return; } bool found = false; for (int i=1; i<(count-1); i++){ int left = n / ((int)pow(10,count-i)); int right = n % ((int)pow(10,count-i-1)); if (checkPrime(left) == 0 && checkPrime(right) == 0){ cout<<i<<"\t"; found = true; } } if (found == false) cout << "-1"; } int main() { int N = 2359; cout<<"All prime divisions of number "<<N<<" are :\n"; primePoints(N); return 0; }
출력
All prime divisions of number 2359 are : 1