■ 순열(Permutation) 구하기

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


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 = itemTextBox.Text.Split(' ');

 

            List<List<string>> resultList = GetPermutationList<string>(itemArray.ToList());

 

            this.permutationListBox.Items.Clear();

 

            foreach(List<string> result in resultList)

            {

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

            }

 

            long permutationCount = GetPermutationCount(itemArray.Length);

 

            this.permutationCountTextBox.Text = permutationCount.ToString();

 

            Debug.Assert(this.permutationListBox.Items.Count == permutationCount);

        }

 

        #endregion

 

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

 

        #region 항목 바꿔넣기 - PermuteItem<T>(itemList, selectionArray, currentArray, resultList, nextPosition)

 

        /// <summary>

        /// 항목 바꿔넣기

        /// </summary>

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

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

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

        /// <param name="currentArray">현재 배열</param>

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

        /// <param name="nextPosition">다음 위치</param>

        private void PermuteItem<T>(List<T> itemList, bool[] selectionArray, T[] currentArray, List<List<T>> resultList, int nextPosition)

        {

            if(nextPosition == itemList.Count)

            {

                resultList.Add(currentArray.ToList());

            }

            else

            {

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

                {

                    if(!selectionArray[i])

                    {

                        selectionArray[i] = true;

 

                        currentArray[nextPosition] = itemList[i];

 

                        PermuteItem<T>(itemList, selectionArray, currentArray, resultList, nextPosition + 1);

 

                        selectionArray[i] = false;

                    }

                }

            }

        }

 

        #endregion

        #region 순열 리스트 구하기 - GetPermutationList<T>(itemList)

 

        /// <summary>

        /// 순열 리스트 구하기

        /// </summary>

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

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

        /// <returns>순열 리스트</returns>

        private List<List<T>> GetPermutationList<T>(List<T> itemList)

        {

            T[] currentArray = new T[itemList.Count];

 

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

 

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

 

            PermuteItem<T>(itemList, selectionArray, currentArray, resultList, 0);

 

            return resultList;

        }

 

        #endregion

        #region 순열 카운트 구하기 - GetPermutationCount(itemCount)

 

        /// <summary>

        /// 순열 카운트 구하기

        /// </summary>

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

        /// <returns>순열 카운트</returns>

        private long GetPermutationCount(long itemCount)

        {

            long result = 1;

 

            for(int i = 2; i <= itemCount; i++)

            {

                result *= i;

            }

 

            return result;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요