用C语言编写程序:输入两个正数m,n,求最大公约数和最小公倍数
- 提问者网友:原来太熟悉了会陌生
- 2021-05-07 18:02
- 五星知识达人网友:空山清雨
- 2021-05-07 18:30
//辗除法:
#include <stdio.h>
void main()
{
int n,m,i,j;
scanf("%d%d",&n,&m);
j=m*n;
//利用辗除法求得最大公约数
while(m!=0)
{
i=n%m;
n=m;
m=i;
}
printf("the Greatest Common Divisor of m,n is %d\n",n);
printf("the Least Common Multiple of m,n is %d\n",j/n);
//最小公倍数等于两数之积除以最大公约数。
}
//递归法:
#include<stdio.h>
int gcd(int m,int n)
{
if (m>n){return gcd(m-n,n);}
if (m<n){return gcd(m,n-m);}
if (m==n){return m;}
}
void main()
{
int m,n,lcm;
scanf("%d%d",&n,&m);
printf("the Greatest Common Divisor of m,n is %d\n",gcd(m,n));
lcm=m*n/gcd(m,n);
printf("the Least Common Multiple of m,n is %d\n",lcm);
}
- 1楼网友:洎扰庸人
- 2021-05-07 21:16
#include <iostream>
int main() { int n,m,i,j; scanf("%d%d",&n,&m); j=m*n; //利用辗除法求得最大公约数 while(m!=0) { i=n%m; n=m; m=i; } printf("the Greatest Common Divisor of m,n is %d\n",n); printf("the Least Common Multiple of m,n is %d\n",j/n); system ("pause");
return 0;
}
- 2楼网友:污到你湿
- 2021-05-07 20:57
#include "stdio.h" void main() {int n,m,s,t; printf("Please Input two Z_nums:"); //提示输入两个整数 scanf("%d%d",&n,&m); s=n*m; while(m!=0) {t=n%m; n=m; m=t;} printf("The Big_y number:%d\n",n); //输出两个整数的最大公约数 printf("The Small_y number:%d\n",s/n); //输出两个整数的最小公倍数
}
- 3楼网友:詩光轨車
- 2021-05-07 19:50
#include <stdio.h>
main()
{ for(;;) { int m=0,n=0,p=0,q=0,i; printf("m=");scanf("%d",&m); printf("n=");scanf("%d",&n); if(m==0||n==0){printf("\nThe End!\n");break;}//当输入有0时程序结束 if(m<n){i=m;m=n;n=i;} for(i=m;i<=m*n;i+=m)if(i%n==0){q=i;break;} p=m*n/q; printf("p=%d,q=%d\n",p,q);//最大公约数和最小公倍数 }
}