下面的程序计算 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;
}