题目:http://acm.hdu.edu.cn/showproblem.php?pid=1164
输入一个数字,将该数字分解成多个素数相乘。
思路:先将65535内的所有素数都保存下来,然后用该数从小到大除,能整除的数输出,更新该数。
#include<iostream>
#include<cmath>
using namespace std;
bool su(int m)
{
for (int i = 2; i <=sqrt(m) ; i++)
if (m%i == 0)
return 0;
return 1;
}
int main()
{
int n,sum=0,j=0;
int a[7000] = { 0 };
for (int i = 2; i <= 65535; i++)
{
if (su(i))
a[j++] = i;//6542个
}
while(cin>>n)
{
if (su(n))
cout << n << endl;
else
{
int m = 0;
while(n>a[m])
{
if (n%a[m] == 0)
{
cout << a[m] << "*";
n = n / a[m];
m = 0;
}
else
m++;
}
cout << n << endl;
}
}
return 0;
}