■ 조합을 구하는 방법을 보여준다.
▶ 조합 구하기 예제 (C#)
1 2 3 4 5 6 7 |
using System; long combination = GetCombination(3, 2); Console.WriteLine(combination); |
▶ 조합 구하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#region 조합 구하기 - GetCombination(totalCount, extractionCount) /// <summary> /// 조합 구하기 /// </summary> /// <param name="totalCount">전체 수</param> /// <param name="extractionCount">추출 수</param> /// <returns>조합</returns> public long GetCombination(int totalCount, int extractionCount) { return GetPermutation(totalCount, extractionCount) / GetFactorial(extractionCount); } #endregion #region 팩토리얼 분할 구하기 - GetFactorialDivision(topFactorial, divisorFactorial) /// <summary> /// 팩토리얼 분할 구하기 /// </summary> /// <param name="topFactorial">상위 팩토리얼</param> /// <param name="divisorFactorial">제수 팩토리얼</param> /// <returns>팩토리얼 분할</returns> private long GetFactorialDivision(int topFactorial, int divisorFactorial) { long result = 1; for(int i = topFactorial; i > divisorFactorial; i--) { result *= i; } return result; } #endregion #region 순열 구하기 - GetPermutation(totalCount, extractionCount) /// <summary> /// 순열 구하기 /// </summary> /// <param name="totalCount">전체 수</param> /// <param name="extractionCount">추출 수</param> /// <returns>순열</returns> private long GetPermutation(int totalCount, int extractionCount) { return GetFactorialDivision(totalCount, totalCount - extractionCount); } #endregion #region 팩토리얼 구하기 - GetFactorial(int i) /// <summary> /// 팩토리얼 구하기 /// </summary> /// <param name="value">값</param> /// <returns>팩토리얼</returns> private long GetFactorial(int value) { if(value <= 1) { return 1; } return value * GetFactorial(value - 1); } #endregion |