边看边写(排列组合及丑数问题 )

#region 字符串排列
    /// <summary>
    /// 输入字符串,打印该字符串的所有排列
    /// </summary>
    class ListOperator<T> where T:IComparable<T>
    {
        private List<T> str;

        public ListOperator() 
        {
            if (str == null)
                str = new List<T>();
        }
        public ListOperator(List<T> list)
        {
            if (str == null)
                str = new List<T>();
            str = list;
        }
        /// <summary>
        /// 排列
        /// </summary>
        /// <param name="m"></param>
        public void Permutation()
        {
            Console.WriteLine();
            if (str == null || str.Count == 0)
                return;
            Permutation(str,0);
            Console.WriteLine();
        }
        public void Permutation(List<T> pStr,int begin)
        {
            if (begin == str.Count)
            {
                pStr.ForEach(l => Console.Write(l));
                Console.Write("  ,  ");
            }
            else
            {
                for (int pCh = begin; pCh != pStr.Count; pCh++)
                {
                    T temp = pStr[pCh];
                    pStr[pCh] = pStr[begin];
                    pStr[begin] = temp;

                    Permutation(pStr, begin + 1);

                    temp = pStr[pCh];
                    pStr[pCh] = pStr[begin];
                    pStr[begin] = temp;
                }
            }
        }
        /// <summary>
        /// 组合
        /// </summary>
        /// <param name="m"></param>
        public void Combine(int m)
        {
            if (str.Count < m)
                return;
            List<T> result =new List<T>();
            Combine(str,ref result,m);
           
        }
        public void Combine(List<T> pStr,ref List<T> result, int m)
        {
            if (m == 0)
            {
                result.ForEach(r => Console.Write(r));
                Console.Write(",");
               
                //result.Add(default(T));
            }
            else
            {
                if (pStr.Count == 0)
                    return;
                result.Add(pStr[0]);
                Combine(pStr.GetRange(1, pStr.Count - 1), ref result, m - 1);
                result.RemoveAt(result.Count - 1);
                Combine(pStr.GetRange(1, pStr.Count - 1), ref result, m);
            }
        }

        /// <summary>
        /// 数组中出现次数超过一半的数字
        /// </summary>
        /// <param name="m"></param>
        #region 方法一,此方法修改了数组
        public T MoreThanHalfByPartion()
        {
            if (str == null || str.Count == 0)
                return default(T);
            int middle = str.Count >> 1;
            int start = 0;
            int end = str.Count - 1;
            int index = Partition(str, start, end);
            while (index != middle)
            {
                if (index < middle)
                {
                    start = index + 1;
                    index = Partition(str, start, end);
                }
                else
                {
                    end = index - 1;
                    index = Partition(str, start,end );
                }
            }
            if (!CheckMoreThanHalf(str[index]))
                Console.WriteLine(str[index] + "的结果并没有超过一半!");
            return str[index];
        }
        public int Partition(List<T> num,int start,int end)
        {
            T pivotkey = num[start];
            
            while (start < end)
            {
                while (num[end].CompareTo(pivotkey)>=0  && start < end)
                    end--;
                T  temp = num[start];
                num[start] = num[end];
                num[end] = temp;
                
                while (num[start].CompareTo(pivotkey) <= 0 && start < end)
                    start++;
                temp = num[start];
                num[start] = num[end];
                num[end] = temp;
            }
            return start;
        }
        #endregion
        #region 方法二,此方法没有修改了数组
        public T MoreThanHalfByFeature()
        {
            if(str==null || str.Count==0)
                return default(T);
            T result = default(T);
            int times = 0;
            str.ForEach(
                delegate (T num)
                {
                    if (times == 0)
                    {
                        result = num;
                        times = 1;
                    }
                    else if (num.CompareTo(result) == 0)
                    {
                        times++;
                    }
                    else
                        times--;
                }
                );
            if (!CheckMoreThanHalf(result))
                Console.WriteLine(result+"的结果并没有超过一半!");
            return result;
        }
        public bool CheckMoreThanHalf(T result)
        {
            int times = 0;
            str.ForEach(
                delegate(T num) 
                {
                    if (num.CompareTo(result) == 0)
                        times++;
            });
            if (times * 2 <= str.Count)
                return false;
            return true;
        }
        #endregion
        #region 丑数问题
        /// <summary>
        /// 我们把只包含因子2、3、5的数称作丑数(ugly number)。求按从小到大的第1500个丑数。例如
        /// 6,8是丑数,14不是,因为它包含因子7.习惯是我们把1作为第一个丑数。
        /// </summary>
        public int GetUglyNumber(int index)
        {
            System.DateTime startTime = System.DateTime.Now;
            
            if(index<=0)
                return 0;
            List<int> pUglyNumber = new List<int>() { 1 };
            int pMultiply2 = 0;
            int pMultiply3 = 0;
            int pMultiply5 = 0;
            int nextUglyIndex =1;
            while (nextUglyIndex < index)
            {
                int min = Min(pUglyNumber[pMultiply2]*2,pUglyNumber[pMultiply3]*3,pUglyNumber[pMultiply5]*5);
                pUglyNumber.Add(min);
                while (pUglyNumber[pMultiply2] * 2 <= pUglyNumber[nextUglyIndex])
                    pMultiply2++;
                while (pUglyNumber[pMultiply3] * 3 <= pUglyNumber[nextUglyIndex])
                    pMultiply3++;
                while (pUglyNumber[pMultiply5] * 5 <= pUglyNumber[nextUglyIndex])
                    pMultiply5++;
                nextUglyIndex++;
            }
            System.DateTime endTime = System.DateTime.Now;
            TimeSpan span = endTime.Subtract(startTime);
            Console.WriteLine("计算时间:"+span.TotalMilliseconds);
            return pUglyNumber.Last();
        }
        public int Min(int number1, int number2, int number3)
        {
            int min = number1 < number2 ? number1 : number2;
            min = number3 < min ? number3 : min;
            return min;
        }
        #endregion
        void Check()
        {
            try
            {
                if (str == null)
                    throw new Exception("str不存在!");
                else if (str.Count == 0)
                    throw new Exception("str没有元素!");
                else
                    throw new Exception("未知错误!");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    #endregion


你可能感兴趣的:(组合,排列,丑数)