随笔 - 6  文章 - 3  trackbacks - 0
<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 1#include <stdio.h>
 2#define MAX 200
 3
 4typedef struct
 5{
 6    int num;
 7    int x[MAX];
 8    int y[MAX];
 9}
 fac;
10
11void 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
54int 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 姚冰 阅读(1866) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理