首页 > 其他 > 详细

最大公约数GCD

时间:2015-03-27 17:36:45      阅读:203      评论:0      收藏:0      [点我收藏+]

http://codeup.cn/problem.php?id=1818 

辗转相除。。已AC

cpp代码:

#include<iostream>
using namespace std;
void _swap(int &aa,int &bb){
    int temp = aa;
    aa = bb;
    bb = temp;
}
int gcd(int &a,int &b){
    if(0 == a)return b;
    if(0 == b)return a;
    if(a <= b)_swap(a,b);
    int c;
    for(c=a%b;c>0;c=a%b){
        a=b;
        b=c;
    }
    return b;
}
int main(){
    int m,n;
    while(cin>>m>>n){
        cout<<gcd(m,n)<<endl;
    }
    return 0;
}


最大公约数GCD

原文:http://blog.csdn.net/messiandzcy/article/details/44676925

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!