프로그래밍 검색 블로그

템플릿 메타 프로그래밍 최대공약수 최대공배수 본문

연습장

템플릿 메타 프로그래밍 최대공약수 최대공배수

코딩조무사 2017. 10. 5. 23:38
최대공약수
1
2
3
4
5
6
7
8
template<unsigned long long L, unsigned long long R>
struct gcd{
    static const unsigned long long value = gcd<R, L % R>::value;
};
template<unsigned long long L>
struct gcd<L, 0LL>{
    static const unsigned long long value = L;
};
cs


최소공배수

1
2
3
4
5
template<unsigned long long L, unsigned long long R>
struct lcm{
    static const unsigned long long value = (L * R) / gcd<L,R>::value;
};
 
cs



테스트

1
2
3
4
5
6
#include <iostream>
using namespace std;
int main(){
    cout << gcd<12,36>::value<<endl;
    cout << lcm<12,36>::value<<endl;
}
cs


Comments