2.4-5、求最大公约数

    技术2022-07-11  75

    5、求最大公约数 【问题描述】 用递归方法求两个数m和n的最大公约数。(m>0,n>0) 【输入格式】 输入二个数,即m和n的值。 【输出格式】 输出最大公约数。 【输入样例】 8 6 【输出样例】 gcd=2

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #define N 1000010 using namespace std; int calculate(int a,int b) { if(b==0) return a; return calculate(b,a%b); } int main() { int a,b; cin>>a>>b; if(a>b) cout<<calculate(a,b)<<endl; else cout<<calculate(b,a)<<endl; return 0; }
    Processed: 0.009, SQL: 9