1
#include <stdio.h>
2
#define MAX 200
3
4
typedef struct
5

{
6
int num;
7
int x[MAX];
8
int y[MAX];
9
} fac;
10
11
void factorization(fac *a, int x) // 质因数分解
12

{
13
int i, count;
14
15
if (x <= 0)
16
{
17
a->num = 0;
18
return;
19
}
20
21
if (x == 1)
22
{
23
a->num = 1;
24
a->x[0] = a->y[0] = 1;
25
return;
26
}
27
28
a->num = 0;
29
30
for (i = 2; i * i <= x; i++)
31
{
32
count = 0;
33
34
while (x % i == 0)
35
{
36
x /= i;
37
count++;
38
}
39
40
if (count != 0)
41
{
42
a->x[a->num] = i;
43
a->y[a->num++] = count;
44
}
45
}
46
47
if (x != 1)
48
{
49
a->x[a->num] = x;
50
a->y[a->num++] = 1;
51
}
52
}
53
54
int main()
55

{
56
int x, i;
57
fac f;
58
59
while (scanf ("%d", &x) != EOF)
60
{
61
factorization(&f, x);
62
63
if (f.num <= 0)
64
{
65
printf("Factorization Error!\n");
66
continue;
67
}
68
69
printf("%d = %d ^ %d", x, f.x[0], f.y[0]);
70
for (i = 1; i < f.num; i++)
71
printf(" * %d ^ %d", f.x[i], f.y[i]);
72
printf("\n");
73
}
74
75
return 0;
76
}
posted on 2011-01-06 00:31
姚冰 阅读(1892)
评论(0) 编辑 收藏 引用