刚刚在做实验,书本里面有代码如下:
View Code
1 #include "stdio.h"
2
3 unsigned long lcm(unsigned a,unsigned b);
4 unsigned long lcm3(unsigned a,unsigned b,unsigned c);
5
6 unsigned long lcm(unsigned a,unsigned b)
7 {
8 unsigned long multiple,c=0;
9
10 multiple = a* b;
11 while(a%b!=0)
12 {
13 c=a%b;
14 a=b;
15 b=c;
16 }
17
18 return multiple/b;
19 }
20
21 unsigned long lcm3(unsigned a,unsigned b,unsigned c)
22 {
23 return lcm(lcm(a,b),c);
24 }
25
26
27 int main()
28 {
29 unsigned long x,y,z;
30 printf("please input three number:");
31 scanf("%ul %ul %ul",&x,&y,&z);
32 printf("%ul %ul %ul",x,y,z);
33 printf("the result is %ul",lcm3(x,y,z));
34 return 0;
35 }
基本思路是说想求三个数的公约数,但在vc++ 6.0 里结果却是错的...
后来不断地排除,发现是在vc++ 6.0 里用不了 %ul,必须改成 %u...
修改后的代码如下:
View Code
1 #include "stdio.h"
2
3 unsigned long lcm(unsigned a,unsigned b);
4 unsigned long lcm3(unsigned a,unsigned b,unsigned c);
5
6 unsigned long lcm(unsigned a,unsigned b)
7 {
8 unsigned long multiple,c=0;
9
10 multiple = a* b;
11 while(a%b!=0)
12 {
13 c=a%b;
14 a=b;
15 b=c;
16 }
17
18 return multiple/b;
19 }
20
21 unsigned long lcm3(unsigned a,unsigned b,unsigned c)
22 {
23 return lcm(lcm(a,b),c);
24 }
25
26
27 int main()
28 {
29 unsigned long x,y,z;
30 printf("please input three number:");
31 scanf("%u %u %u",&x,&y,&z);
32 printf("%u %u %u",x,y,z);
33 printf("the result is %u",lcm3(x,y,z));
34 return 0;
35 }