在c ++ sans cmath库中的GCD函数 [英] GCD function in c++ sans cmath library

查看:545
本文介绍了在c ++ sans cmath库中的GCD函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个混合数字类,需要一个快速,简单的最大公约数函数。任何人都可以给我的代码或链接到代码?

I'm writing a mixed numeral class and need a quick and easy 'greatest common divisor' function. Can anyone give me the code or a link to the code?

推荐答案

我很想投票关闭 - 似乎很难相信一个实现将很难找到,

I'm tempted to vote to close -- it seems difficult to believe that an implementation would be hard to find, but who knows for sure.

unsigned GCD(unsigned u, unsigned v) {
    while ( v != 0) {
        unsigned r = u % v;
        u = v;
        v = r;
    }
    return u;
}

这篇关于在c ++ sans cmath库中的GCD函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆