2 条题解

  • 1
    @ 2025-1-23 16:08:55

    辗转相减法(欧几里得算法)

    #include<bits/stdc++.h>
    using namespace std;
    
    int gcd(int a,int b){
    	if (a==b) return a;//当a和b相等时直接返回a或b即可
    	else if (a>b) {
    		return gcd(b,a-b);
    	}else{
    		return gcd(a,b-a);
    	}
    }
    
    int main(){
    	int a,b;
    	cin>>a>>b;
    	cout<<gcd(a,b);
    }
    

    辗转相除法

    #include<bits/stdc++.h>
    using namespace std;
    
    int gcd2(int a,int b){
    	if (b==0) return a;
    	else return gcd2(b,a%b);
    }
    
    int main(){
    	int a,b;
    	cin>>a>>b;
    	cout<<gcd2(a,b);
    }
    
    • 1
      @ 2025-1-20 15:44:40

      #include using namespace std; int f(int x,int y){ if(x%y==0){ return y; } if(x%y>0){ return f(y,x%y); } }

      int main(){ int x,y; cin>>x>>y; cout<<f(x,y)<<endl; return 0; }

      • 1

      最大公约数(辗转相除法、欧几里得算法)

      信息

      ID
      142
      时间
      1000ms
      内存
      256MiB
      难度
      8
      标签
      递交数
      11
      已通过
      8
      上传者