2021-03-26习题4-7 最大公约数和最小公倍数 (15 分)

方法一:
#include<stdio.h>
#include<math.h>
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}
int main(){
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		int G=gcd(n,m);
	    int t=n*m;
	    printf("%d %d\n",G,t/G);
	}
	return 0;
}
//方法二:
#include<stdio.h>
#include<math.h>
int min(int a,int b){
	return a>b?b:a;
}
int Stein_gcd(int a,int b){
	if(a==0){
		return b;
	}
	if(b==0){
		return a;
	}
	if(a%2==0&&b%2==0){
		return 2*Stein_gcd(a>>1,b>>1);
	}else if(a%2==0&&b%2!=0){
		Stein_gcd(a>>1,b);
	}else if(a%2!=0&&b%2==0){
		return Stein_gcd(a,b>>1);
	}else{
		return Stein_gcd(abs(a-b),min(a,b));
	}
}
int main(){
	int t;
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		int G=Stein_gcd(n,m);
		int t=n*m;
	    printf("%d %d\n",G,t/G);
	}
	return 0;
}
//方法三:
#include<stdio.h>
#include<math.h>
int gcd(int a,int b){
	while(b){
		int t=b;
		b=a%b;
		a=t;
	}
	return a;
}
int main(){
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		int t=n*m;
		int G=gcd(n,m);
		printf("%d %d\n",G,t/G);
	}
	return 0;
} 

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页