c++ G - Sphenic numbers Gym - 101243G SDUT
程序员文章站
2022-07-15 10:45:32
...
题目大意:给定一个整数判断是否为三个不同素数的乘积
解题思路:
因为是将一个整数分解成3个素数,所以我们想到唯一分解定理:
任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积 N=P1a1P2a2P3a3…Pnan,这里P1<P2<P3…<Pn均为质数,其中指数ai是正整数。这样的分解称为 N 的标准分解式.
#include<bits/stdc++.h>
using namespace std;
/*自定义函数*/
int fenjie(int n);
int main()
{
int n;
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
while (cin >> n)
{
int cnt = fenjie(n);
if (cnt == 3)
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
}
/*自定义函数*/
int fenjie(int n)
{
int cnt = 0;
int k = sqrt(n);
for (int i = 2; i <= k; i++)
{
if (n % i == 0)
{
cnt++;
}
while (n % i == 0)
{
n /= i;
}
}
if (n > 1)
{
cnt++;
}
return cnt;
}