使用模板计算2个数字的LCM

时间:2016-03-29 08:31:52

标签: c++ lcm

下面的程序计算2个数字的LCM,预期输出为216,输入为54和24,但我得到57.

有人可以提供帮助吗,请告诉我下面的代码段有什么不妥。

/* *********** /
*** LCM ******/
/**************/
template<bool cond, int V1, int V2>
struct IfCond
{
    enum
    {
        value = V1
    };
};

template<int V1, int V2>
struct IfCond<0, V1, V2>
{
    enum
    {
        value = V2
    };
};

template<int V1, int V2>
struct findMax
{
    enum
    {
        result = V1 > V2,
        value = IfCond<result, V1, V2>::value
    };
};

template<int V1, int V2, int max>
struct findLCM
{
    enum
    {
        result = findLCM<max % V1, max % V2, max+1>::result
    };
};

template<int V2, int max>
struct findLCM<0, V2, max>
{
    enum
    {
        result = findLCM<0, max % V2, max+1>::result
    };
};

template<int V1, int max>
struct findLCM<V1, 0, max>
{
    enum
    {
        result = findLCM<max % V1, 0, max+1>::result
    };
};

template<int max>
struct findLCM<0, 0, max>
{
    enum
    {
        result = max
    };
};

int main()
{
    std::cout<< findLCM<54, 24, findMax<54, 24>::value>::result << std::endl;
}

3 个答案:

答案 0 :(得分:3)

您需要的是:

template<int V1, int V2>
struct findGCD
{
    enum { result = findGCD<V2, V1%V2>::result };
};

template<int V1>
struct findGCD<V1,0>
{
    enum { result = V1 };
};

template<int V1, int V2>
struct findLCM
{
   enum { result = (V1/findGCD<V1,V2>::result) * V2 };
};

int main()
{
    std::cout<< findGCD<54, 24>::result << std::endl;  // 6
    std::cout<< findLCM<54, 24>::result << std::endl;  // 216
}

如果你想通过线性搜索来做,你需要像:

template <int V1, int V2, bool finished, int target>
struct findLCMHelper
{
    enum { result = findLCMHelper<V1, V2, 
                        target%V1 == 0 && target%V2==0,
                        target+1>::result };
};

template<int V1, int V2, int target>
struct findLCM<V1, V2, true, target>
{
    enum { result = target-1 };  // Correct overshoot
};

template<int V1, int V2>
struct findLCM
{
    enum { target = findMax<V1,V2>::value,
           result = findLCMHelper<V1, V2, 
                        target%V1 == 0 && target%V2==0,
                        target+1>::result };
};

我不喜欢那种双重测试。必须有一种方法来重构它。我希望这会破坏编译器 - 它将导致150多个奇怪的模板实例化。

令人惊讶的是,cpp.sh拒绝破解 - 即使是5400,24

答案 1 :(得分:0)

我认为你会做这样的事情

#include <iostream>

template <int X, int Y>
struct findGDMH
 { static int const result = findGDMH<Y, X%Y>::result; };

template <int X>
struct findGDMH<X, 0>
 { static int const result = X; };

template <int X, int Y>
struct findGDM
 {
   static bool const largerIsX = (X > Y);
   static int  const A         = (largerIsX ? X : Y);
   static int  const B         = (largerIsX ? Y : X);
   static int  const result    = findGDMH<A, B>::result;
 };

template <int X>
struct findABS
 { static int const  result = (X > 0 ? X : -X); };

template <int X, int Y>
struct findLCM
 { static int const  result = findABS<X*Y>::result
   / findGDM<findABS<X>::result, findABS<Y>::result>::result; };

template <int X>
struct findLCM<X, 0>
 { static int const  result = 0; };

template <int X>
struct findLCM<0, X>
 { static int const  result = 0; };

template <>
struct findLCM<0, 0>
 { static int const  result = 0; };


int main() 
{
    std::cout<< findLCM<54, 24>::result << std::endl;

    return 0;
}

答案 2 :(得分:0)

使用C ++ 11,您可以使用constexpr函数:

constexpr unsigned abs(int const op) {
    return op >= 0 ? op : -op;
}

constexpr unsigned gcd(unsigned const a, unsigned const b) {
    return b ? gcd(b, a % b) : a;
}

constexpr unsigned lcm(int const a, int const b) {
    return abs(a * b) / gcd(abs(a), abs(b));
}

如果您想使用模板:

template <int Op>
struct abs {
    static constexpr unsigned const value{Op >= 0 ? Op : -Op};
};

template <unsigned A, unsigned B>
static gcd : gcd<B, A % B> {};

template <unsigned A>
static gcd<A, 0> {
    static constexpr unsigned const value{A};
};

template <int A, int B>
static lcm {
    static constexpr unsigned const value{
        abs<A * B>::value / gcd<abs<A>::value, abs<B>::value>::value
    };
};