pku1142 Smith Numbers

http://poj.org/problem?id=1142

数学,分解质因数

 1 #include <stdio.h>
 2 #include <math.h>
 3 #define N 10000000
 4 
 5 int a[N], b[N], tot;
 6 
 7 int f(int x)
 8 {
 9     int sum = 0;
10     while(x)
11     {
12         sum += x%10;
13         x /= 10;
14     }
15     return sum;
16 }
17 
18 int factor(int n)
19 {
20     int sum, j, temp, i, now;
21     int flag = 0;
22     if(n == 2)
23     {
24         return 0;
25     }
26     temp = (int) ((double)sqrt(n) + 1);
27     tot = 0;
28     now = n;
29     for(i=2; i<=temp; i++)
30     {
31         if(n%i == 0)
32         {
33             flag = 1;
34         }
35         if(now%i == 0)
36         {
37             a[++tot] = i;
38             b[tot] = 0;
39             while(now%i == 0)
40             {
41                 ++ b[tot];
42                 now /= i;
43             }
44         }
45     }
46     if(flag == 0)
47     {
48         return 0;
49     }
50     if(now != 1)
51     {
52         a[++tot] = now;
53         b[tot] = 1;
54     }
55     sum = 0;
56     for(i=1; i<=tot; i++)
57     {
58         sum += f(a[i])*b[i];
59     }
60     if(sum == f(n))
61     {
62         return 1;
63     }
64     return 0;
65 }
66 
67 int main()
68 {
69     int n, i;
70     while(scanf("%d", &n), n)
71     {
72         for(i=n+1; 1; i++)
73         {
74             if(factor(i))
75             {
76                 printf("%d\n", i);
77                 break;
78             }
79         }
80     }
81     return 0;
82 }
原文地址:https://www.cnblogs.com/yuan1991/p/pku1142.html