社区讨论

为什么辗转相除法能过,而更相减损术不能过

B3736[信息与未来 2018] 最大公约数参与者 4已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mi5nl3h4
此快照首次捕获于
2025/11/19 15:00
3 个月前
此快照最后确认于
2025/11/19 15:20
3 个月前
查看原帖
更相减损术
CPP
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int gcd(int a,int b){
	while(a!=b){
		if(a>b) a-=b;
		else b-=a;
	}
	return a;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>a>>b>>c;
	int x=gcd(a,b);
	cout<<gcd(x,b);
	return 0;
} 
辗转相除法
CPP
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int gcd(int a,int b){
	if(b){
		return gcd(b,a%b); 
	}else{
		return a;
	}
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>a>>b>>c;
	int x=gcd(a,b);
	cout<<gcd(x,b);
	return 0;
} 

回复

4 条回复,欢迎继续交流。

正在加载回复...