프로그래밍 퀴즈 – Factorial

  • Post author:
  • Post category:칼럼
  • Post comments:0 Comments
  • Post last modified:January 26, 2016

문제

팩토리얼 !n 을 구현해보라.

컴파일 타임에 값 구하기

wikipedia:Template_metaprogramming|템플릿 메타프로그래밍에 제시된 팩토리얼은 다음과 같다.

template <int N>
struct Factorial 
{
    enum { value = N * Factorial<N - 1>::value };
};
 
template <>
struct Factorial<0> 
{
    enum { value = 1 };
};
 
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}

팩토리얼 값을 컴파일 타임에 구하기 때문에 성능 향상을 꾀할 때 쓴다. 다만 n 값이 미리 정해진 경우에만 쓸모가 있다. 만약 n 값을 런타임에 임의로 결정하게 되면 이 방법은 통하지 않는다.

런타임에 값 구하기

글 써야 함!

Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments