辗转相除法递归与非递归代码c++

  1. 非递归
int gcd(int n, int m){
	if(n0){
		int t = n%m;
		n = m;
		m = t;
	}
	return n;
}
  1. 递归
int gcd(int n, int m){
	if(n>=m){
		if(m==0) return n;
		int t = n%m;
		n = m;
		m = t;
		gcd(n,m);
	}else{
		return gcd(m,n);
	}
}

你可能感兴趣的:(杂谈)