성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - 모든 경우의 수를 조합하는 코드 (2)</h1> <p> 지난번 글에서 모든 경우의 수를 조합하는 코드를 알아봤는데요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - 모든 경우의 수를 조합하는 코드 (1) ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/10977'>http://www.sysnet.pe.kr/2/0/10977</a> </pre> <br /> 글을 보시면 아시겠지만, "모든 경우의 수"는 2<sup>n</sup>과 같습니다. 2의 n승이니, 이는 포화 2진 트리 형식으로도 표현이 가능합니다. 가령 2개의 요소를 갖는 경우의 수를 보면 다음과 같이 트리로 표현됩니다.<br /> <br /> <img alt='bin_tree_combination_1.png' src='/SysWebRes/bbs/bin_tree_combination_1.png' /><br /> <br /> 루트에서부터 하위 리프 노드로 가면서 (또는, 거꾸로) 이어지는 숫자의 배열을 나열해 보면 경우의 수와 동일한 구성을 볼 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0 [0 0] 0 [0 1] 0 [1 0] 0 [1 1] </pre> <br /> 따라서, 경우의 수를 탐색하는데 재귀호출로 이렇게 표현하는 것도 가능합니다. (트리 구성은 임의 구현이 가능한데, 아래의 코드는 그 하나의 사례라고 보시면 됩니다.)<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public class Node { public readonly Node Parent; public readonly int Index; public Node(Node parent, int index) { this.Parent = parent; this.Index = index; } } public class Combination { readonly int _depth; readonly int[] _sourceList; List<Node> _leafNodes = new List<Node>(); public Combination(int [] elems) { _sourceList = elems; _depth = _sourceList.Length; Prepare(); } void Prepare() { VisitElement(1, new Node(null, 1)); VisitElement(1, new Node(null, 0)); } private void VisitElement(int depth, Node node) { if (depth == _depth) { _leafNodes.Add(node); return; } VisitElement(depth + 1, new Node(node, 1)); VisitElement(depth + 1, new Node(node, 0)); } internal IEnumerable<int []> Combinations() { foreach (Node leaf in _leafNodes) { Node node = leaf; List<int> elems = new List<int>(); int index = 0; while (node != null) { if (node.Index == 1) { elems.Add(_sourceList[index]); } index++; node = node.Parent; } yield return elems.ToArray(); } } } </pre> <br /> 사용은 이렇게 할 수 있고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > static void Main(string[] args) { int[] list = new int[] { 200, 300, 500, 600 }; Combination c = new Combination(list); foreach (var item in c.Combinations()) { PrintElems(item); } } private static void PrintElems(int[] elems) { Console.Write("{ "); foreach (var elem in elems) { Console.Write(elem + ", "); } Console.WriteLine(" }"); } </pre> <br /> 출력 결과는 모든 경우의 수입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > { 200, 300, 500, 600, } { 300, 500, 600, } { 200, 500, 600, } { 500, 600, } { 200, 300, 600, } { 300, 600, } { 200, 600, } { 600, } { 200, 300, 500, } { 300, 500, } { 200, 500, } { 500, } { 200, 300, } { 300, } { 200, } { } </pre> <br /> 역시 개념만 알아두면, 언제든 쉽게 만들 수 있는 코드입니다.<br /> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1035&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1382
(왼쪽의 숫자를 입력해야 합니다.)