整数分解素数
1 #include <string.h>
2 #include <stdio.h>
3 #include <math.h>
4 #include <iostream>
5 using namespace std;
6 const int maxn=100005;
7 bool prime[maxn];
8 int k=0;
9 int p[maxn];
10 void isprime()
11 {
12
13 memset(prime,1,sizeof(prime));
14 prime[0]=prime[1]=0;
15 int sq=sqrt((double)maxn);
16 for(int i=2;i<=sq;i++)
17 {
18 if(prime[i])
19 {
20 p[k++]=i;
21 for(int j=i*i;j<=maxn;j+=i)
22 {
23 prime[j]=0;
24 }
25 }
26
27 }
28 }
29 void make(int n)
30 {
31 if(prime[n])
32 {
33 printf("%d\n",n);
34 return;
35 }
36 for(int i=0;i<k;i++)
37 {
38 if(n%p[i]==0)
39 {
40 printf("%d*",p[i]);
41 make(n/p[i]);
42 return;
43 }
44 }
45 }
46 int main()
47 {
48 isprime();
49 int n;
50 while(scanf("%d",&n)!=EOF)
51 {
52 make(n);
53 }
54 return 0;
55 }