ADV-92-算法提高-求最大公约数.

来源:互联网 发布:客户数据库 编辑:程序博客网 时间:2024/06/05 04:18
算法提高 求最大公约数
编写一函数gcd,求两个正整数的最大公约数。
样例输入: 
5 15
样例输出:
5
样例输入: 
7 2
样例输出:
1


思路:求出最小公倍数,然后用两个数之乘积去除以最小公倍数即 最大公约数


#include <iostream>#include <algorithm>using namespace std;int main() {    int a,b,c;    cin >>a>>b;    int d=a*b;    for(int i=a;;i++)    if(i%a==0 && i%b==0) {    c=i;    break;}        cout<<d/c<<endl;    return 0;}


0 0