첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 조합(Combination) 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.generateButton.Click += generateButton_Click;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 생성 버튼 클릭시 처리하기 - generateButton_Click(sender, e)

 

        /// <summary>

        /// 생성 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void generateButton_Click(object sender, EventArgs e)

        {

            string[] itemArray = txtItems.Text.Split(' ');

 

            int selectionCount = int.Parse(this.selectionCountTextBox.Text);

 

            List<List<string>> resultList = GetCombinationList<string>(itemArray.ToList(), selectionCount);

 

            this.combinationListBox.Items.Clear();

 

            foreach(List<string> result in resultList)

            {

                this.combinationListBox.Items.Add(string.Join(" ", result.ToArray()));

            }

 

            decimal combinationCount = GetCombinationCount(itemArray.Length, selectionCount);

 

            this.combinationCountTextBox.Text = combinationCount.ToString();

 

            Debug.Assert(this.combinationListBox.Items.Count == combinationCount);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 항목 선택하기 - SelectItem<T>(itemList, selectionArray, resultList, selectionCount, firstItemIndex)

 

        /// <summary>

        /// 항목 선택하기

        /// </summary>

        /// <typeparam name="T">항목 타입</typeparam>

        /// <param name="itemList">항목 리스트</param>

        /// <param name="selectionArray">선택 배열</param>

        /// <param name="resultList">결과 리스트</param>

        /// <param name="selectionCount">선택 카운트</param>

        /// <param name="firstItemIndex">첫번째 항목 인덱스</param>

        private void SelectItem<T>(List<T> itemList, bool[] selectionArray, List<List<T>> resultList, int selectionCount, int firstItemIndex)

        {

            if(selectionCount == 0)

            {

                List<T> selectionList = new List<T>();

 

                for(int i = 0; i < itemList.Count; i++)

                {

                    if(selectionArray[i])

                    {

                        selectionList.Add(itemList[i]);

                    }

                }

 

                resultList.Add(selectionList);

            }

            else

            {

                for(int i = firstItemIndex; i < itemList.Count; i++)

                {

                    selectionArray[i] = true;

 

                    SelectItem(itemList, selectionArray, resultList, selectionCount - 1, i + 1);

 

                    selectionArray[i] = false;

                }

            }

        }

 

        #endregion

        #region 조합 리스트 구하기 - GetCombinationList<T>(itemList, selectionCount)

 

        /// <summary>

        /// 조합 리스트 구하기

        /// </summary>

        /// <typeparam name="T">항목 타입</typeparam>

        /// <param name="itemList">항목 리스트</param>

        /// <param name="selectionCount">선택 카운트</param>

        /// <returns>조합 리스트</returns>

        private List<List<T>> GetCombinationList<T>(List<T> itemList, int selectionCount)

        {

            bool[] selectionArray = new bool[itemList.Count];

 

            List<List<T>> resultList = new List<List<T>>();

 

            SelectItem<T>(itemList, selectionArray, resultList, selectionCount, 0);

 

            return resultList;

        }

 

        #endregion

        #region 조합 카운트 구하기 - GetCombinationCount(itemCount, selectionCount)

 

        /// <summary>

        /// 조합 카운트 구하기

        /// </summary>

        /// <param name="itemCount">항목 카운트</param>

        /// <param name="selectionCount">선택 카운트</param>

        /// <returns>조합 카운트</returns>

        private decimal GetCombinationCount(decimal itemCount, decimal selectionCount)

        {

            Debug.Assert(itemCount >= 0);

            Debug.Assert(selectionCount >= 0);

            Debug.Assert(itemCount >= selectionCount);

 

            decimal result = 1;

 

            for(int i = 1; i <= selectionCount; i++)

            {

                result *= itemCount - (selectionCount - i);

                result /= i;

            }

 

            return result;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요