分解素因子

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1634

将一个数表示成素数相乘的形式,其中素数从小到大排列。

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 const int N=65536;
 5 int a[N];
 6 void is_prime()
 7 {
 8     a[1] = 0,a[2] = 1;
 9     for (int i = 3; i <= N; i ++)
10     {
11         a[i] = i%2;
12     }
13     for (int i = 3; i <= sqrt(65536); i ++)
14     {
15         if (a[i])
16         {
17             for (int j = 2*i; j <= 65536; j +=i)
18             {
19                 a[j] = 0;
20             }
21         }
22     }
23 }
24 int main()
25 {
26     int x,n,b[N+N];
27     scanf("%d",&n);
28     is_prime();
29     while(n--)
30     {
31         scanf("%d",&x);
32 
33         int k = 0;
34         for (int i = 2; i <= x; i ++)
35         {
36 
37             if (x==1||i > x)
38                 break;
39             while(a[i])
40             {
41                 if(x%i==0)
42                 {
43                     x = x/i;
44                     b[k++] = i;
45 
46                 }
47                 else
48                     break;
49             }
50         }
51         for (int i = 0; i < k; i ++)
52         {
53             if (i==0)
54                 printf("%d",b[i]);
55             else
56                 printf("*%d",b[i]);
57         }
58         puts("");
59     }
60     return 0;
61 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3279311.html