-
[C++] combination.cpp 조합값 계산하기C++/clone code 2022. 5. 22. 21:09
Power C++ 교재 151p.
수학적인 조합 값을 구하는 예제
[조건] 사용자 정의 함수가 다른 사용자 정의 함수를 호출
// // main.cpp // function // // Created by yeonsu on 2022/05/22. // #include <iostream> using namespace std; /*함수 원형 정의*/ int get_integer(void); //사용자로부터 정수를 입력받는 함수 int combination (int n, int r); //조합 계산 함수 int factorial(int n); //팩토리얼 계산 함수 int main(){ /*정수를 입력받는 함수의 반환값을 변수 a,b에 저장*/ int a, b; a = get_integer(); b = get_integer(); cout << "C(" << a << "," << b << ") = " << combination(a, b) << endl; system("pause"); return 0; } /*함수 정의*/ int get_integer(void){ int n; cout << "정수를 입력하시오: "; cin >> n; return n; } int factorial(int n){ int i; int result = 1; //result의 초기값을 반드시 1이어야 한다. for(i = 1; i <= n; i++) result *= i; return result; } int combination(int n, int r){ return (factorial(n)/(factorial(r) * factorial (n-r))); }
실행 결과
'C++ > clone code' 카테고리의 다른 글
[C++] find_max_n_min.cpp 최댓값, 최솟값 구하는 프로그램(배열 활용) (0) 2022.05.23 [C++] sum_matrix.cpp 3x3 행렬 덧셈 프로그램 (0) 2022.05.23 [C++] 2차원 배열의 이해(two dimensional array) (0) 2022.05.23 [C++] average.cpp 배열을 활용한 평균 계산 (0) 2022.05.22 [C++] frequency.cpp 데이터의 빈도수 계산 (0) 2022.05.22