编写一个函数,求出一个给定函数字的所有因子,如72=2*2*2*3*3

2024-12-19 01:38:02
推荐回答(3个)
回答1:

#include

void Factor(int n)
{
if(n == 0 || n == 1)
{
printf("%d = %d", n, n);
return;
}

int i = 2;
printf("%d =", n);
while(1)
{
if(n%i == 0)
{
n /= i;
if(n == 1)
{
printf(" %d", i);
break;
}
else
printf(" %d *", i);
}
else
++i;
}
}

int main()
{
for(int i = 0; i <= 72; ++i)
{
Factor(i);
printf("\n");
}
}

0 = 0
1 = 1
2 = 2
3 = 3
4 = 2 * 2
5 = 5
6 = 2 * 3
7 = 7
8 = 2 * 2 * 2
9 = 3 * 3
10 = 2 * 5
11 = 11
12 = 2 * 2 * 3
13 = 13
14 = 2 * 7
15 = 3 * 5
16 = 2 * 2 * 2 * 2
17 = 17
18 = 2 * 3 * 3
19 = 19
20 = 2 * 2 * 5
21 = 3 * 7
22 = 2 * 11
23 = 23
24 = 2 * 2 * 2 * 3
25 = 5 * 5
26 = 2 * 13
27 = 3 * 3 * 3
28 = 2 * 2 * 7
29 = 29
30 = 2 * 3 * 5
31 = 31
32 = 2 * 2 * 2 * 2 * 2
33 = 3 * 11
34 = 2 * 17
35 = 5 * 7
36 = 2 * 2 * 3 * 3
37 = 37
38 = 2 * 19
39 = 3 * 13
40 = 2 * 2 * 2 * 5
41 = 41
42 = 2 * 3 * 7
43 = 43
44 = 2 * 2 * 11
45 = 3 * 3 * 5
46 = 2 * 23
47 = 47
48 = 2 * 2 * 2 * 2 * 3
49 = 7 * 7
50 = 2 * 5 * 5
51 = 3 * 17
52 = 2 * 2 * 13
53 = 53
54 = 2 * 3 * 3 * 3
55 = 5 * 11
56 = 2 * 2 * 2 * 7
57 = 3 * 19
58 = 2 * 29
59 = 59
60 = 2 * 2 * 3 * 5
61 = 61
62 = 2 * 31
63 = 3 * 3 * 7
64 = 2 * 2 * 2 * 2 * 2 * 2
65 = 5 * 13
66 = 2 * 3 * 11
67 = 67
68 = 2 * 2 * 17
69 = 3 * 23
70 = 2 * 5 * 7
71 = 71
72 = 2 * 2 * 2 * 3 * 3

回答2:

#include
yinzi(int a)
{
int i;
for(i=2;i<=(a+1)/2;i++)
while(!(a%i))
{printf("%d\n",i);
a/=i;
}
}

void main()
{
int j;
scanf("%d",&j);
yinzi(j);
}

回答3:

#include
#include

void fenjia(int a)
{
for (int i=2; i<=a; ++i)
while (a%i==0)
{
printf("%d ",i);
a/=i;
}
printf("\n");
}

int main()
{
int a=0;
printf("输入一个整数: ");
scanf("%d",&a);
fenjia(a);
return 0;
}