以下のプログラムは 2 つの数値の LCM を計算します。予想される出力は 54 と 24 の入力で 216 ですが、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;
}