#include<stdio.h>
#include<math.h>
main()
{int i,j,k,l,a,b;
printf("\nplease input two number:\n");
scanf("%d%d",&a,&b);
i=1;j=1;
while(a/i!=b/j)
{while(a/i>b/j)
i++;
while(a/i<b/j)
j++;
}
printf("The zuidagongyueshu of two number is %d\n",a/i);
i=1;j=1;
while(a*i!=b*j)
{while(a*i>b*j)
j++;
while(a*i<b*j)
i++;
}
printf("The zuixiaogongbeishu of two number is %d\n",a*i);
}
[此贴子已经被作者于2006-8-14 21:07:37编辑过]
----------------解决方案--------------------------------------------------------
刚做了这个的练习题,这是偶写的
#include<stdio.h>
int gcd(int m,int n) //最大公约数
{
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
return m;
}
int lcm(int m,int n) //最小公倍数
{
int result;
result=(m*n)/gcd(m,n);
return result;
}
int main()
{
int u,v,common_divisor,common_multiple;
printf("Enter two non-zero integer:");
scanf("%i %i",&u,&v);
if(u*v==0||u<0||v<0)
return 0;
else
{
common_divisor=gcd(u,v);
common_multiple=lcm(u,v);
printf("\n greatest common_divisor = %i \n greates tcommon_multiple = %i\n",common_divisor,common_multiple);
return 1;
}
}
----------------解决方案--------------------------------------------------------
楼主,你的程序,当我输入5和6时,
输出的最大公约数怎么会是2呀
----------------解决方案--------------------------------------------------------
楼主,你的程序,当我输入5和6时,
输出的最大公约数怎么会是2呀
----------------解决方案--------------------------------------------------------
说得对,是因为,a/i,b/j都只能是整型,所以除了问题。待我改改
----------------解决方案--------------------------------------------------------
/*最大公约数,最小公倍数*/
#include<stdio.h>
#include<math.h>
main()
{double i,j;
int a,b;
printf("\nplease input two number:\n");
scanf("%d%d",&a,&b);
i=1;j=1;
while(a/i!=b/j)
{while(a/i>b/j)
i++;
while(a/i<b/j)
j++;
}
printf("The zuidagongyueshu of two number is %f\n",a/i);
i=1;j=1;
while(a*i!=b*j)
{while(a*i>b*j)
j++;
while(a*i<b*j)
i++;
}
printf("The zuixiaogongbeishu of two number is %f\n",a*i);
}
这样就对了。分母必须要比分子的类型复杂。int/double.
[此贴子已经被作者于2006-8-25 21:32:52编辑过]
----------------解决方案--------------------------------------------------------
int/double,不能是double/int
----------------解决方案--------------------------------------------------------
int gcd(int im,int in)
{
while(0!=im)
{
im%=in;
if(0==im) return in;
in%=im;
if(0==in) return im;
}
}
----------------解决方案--------------------------------------------------------