杭电1176 Eddy's research I(整数的质分解)
Problem Description
Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .
Input
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
Output
You have to print a line in the output for each entry with the answer to the previous question.
Sample Input
11 9412
Sample Output
11 2*2*13*181
Author
eddy
#include<iostream> using namespace std; #include<math.h> int a[10000]; int main() { int i,t,n,p,q,j,b[10000]; p=0; //计算65535/2以内的素数,对于一个确定的数,在它的分解中不会出现大于其二分之一的质因子, //若有大于其二分之一的因子,请问其另一个因子是几(显然另一个因子不存在) for(i=2;i<=65535/2;i++)//这样只需要记录65535/2以内的素数,而不是记录65535以内的素数,从而节省大量时间 { q=0; for(j=2;j<=sqrt(double(i));j++)//此处必须将i利用double(i)转换成double类型,因为sqrt(double a)
{ if(i%j==0) {q++;break;}//利用试除法判断i是否是素数,效率较低 } if(q==0) {a[p]=i;p++;} } //数组a里面保存的全是素数 //for(i=0;i<100;i++) // cout<<a[i]<<" "; //} while(cin>>n) { t=0; while(n>1) { for(i=0;i<p;i++) { if(n%a[i]==0) { b[t]=a[i]; t++; n=n/a[i]; break; } } } if(t==1)cout<<b[0]; else { for(i=0;i<t;i++) { if(i==0) { cout<<b[0];continue; } cout<<"*"<<b[i]; } } } return 0; }

浙公网安备 33010602011771号