最大公约数 欧几里得算法 连续监测算法

    技术2022-05-20  72

    /** *最大公约数的欧几里得算法 **/ #include"iostream.h" int gcd(int m, int n); int gcdTest(int m, int n); int main() { int m , n, r = 0; cin >> m >> n; cout << "gcd(" << m << "," << n << ") =" << gcd(m,n)<<endl; cout << "gcdTest(" << m << "," << n << ") =" << gcdTest(m,n)<<endl; return 0; } //欧几里得算法,辗转相除 int gcd(int am,int an) { int temp =0; while(an) { temp = am % an; am = an; an = temp; } return am; } //连续监测算法,从最小者开始检测,每次减一 int gcdTest(int m, int n) { int min = 0; if(m >= n) min = n; else min = m; int tag = 1; while(tag != 0) { if(0 == m % min) if(0 == n % min) { tag = 0; return min; } else min--; else min--; } }


    最新回复(0)