3 条题解

  • 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; }

      • 0
        @ 2025-8-22 21:48:26

        为达到刷题数量,直接把前面最大公因数的AC代码拿过来了

        #include <bits/stdc++.h>
        using namespace std;
        int gcd(int x,int y)
        {
            if(y==0)
            {
                return x;
            }
            else
            {
                return gcd(y,x%y);
            }
        }
        int main()
        {
            int x,y;
            cin>>x>>y;
            cout<<gcd(y,x%y);
        }
        
        • 1

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

        信息

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