■ 조합을 구하는 방법을 보여준다.
▶ 조합 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; List<string> sourceList = new List<string> { "가", "나", "다", "라" }; Combination combination = new Combination(sourceList, 3); foreach(string[] resultElementArray in combination.GetResult()) { foreach(string element in resultElementArray) { Console.Write(element + " "); } Console.WriteLine(); } |
▶ 조합 구하기 (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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
using System.Collections.Generic; /// <summary> /// 조합 /// </summary> public class Combination { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 리스트 /// </summary> private List<string> sourceList; /// <summary> /// 추출 카운트 /// </summary> private int extractCount; /// <summary> /// 시작 인덱스 /// </summary> private ulong startIndex; /// <summary> /// 종료 인덱스 /// </summary> private ulong endIndex; /// <summary> /// 케이스 배열 /// </summary> private string[] caseArray; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Combination(sourceList, extractCount) /// <summary> /// 생성자 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <param name="extractCount">추출 카운트</param> public Combination(List<string> sourceList, int extractCount) { this.sourceList = sourceList; this.extractCount = extractCount; this.startIndex = (ulong)((1 << this.extractCount) - 1); this.endIndex = this.startIndex << (this.sourceList.Count - this.extractCount); this.caseArray = new string[this.extractCount]; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 결과 구하기 - GetResult() /// <summary> /// 결과 구하기 /// </summary> /// <returns>문자열 열거형</returns> public IEnumerable<string[]> GetResult() { ulong currentIndex = this.startIndex; while(true) { int caseIndex = 0; for(int i = 0; i < this.sourceList.Count; i++) { ulong mask = (ulong)1 << i; if((currentIndex & mask) == mask) { caseArray[caseIndex ++] = this.sourceList[i]; } } yield return caseArray; if(currentIndex == this.endIndex) { yield break; } currentIndex = SNOOB(currentIndex); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region SNOOB(x) /// <summary> /// SNOOB /// </summary> /// <param name="x">X</param> /// <returns>SNOOB</returns> private ulong SNOOB(ulong x) { ulong smallest; ulong ripple; ulong ones; smallest = x & (ulong)-(long)x; ripple = x + smallest; ones = x ^ ripple; ones = (ones >> 2) / smallest; return ripple | ones; } #endregion } |