日期:2014-05-20  浏览次数:20840 次

最后一帖
求字符串在给定长度情况下所有排列方式
如str="abc"
min=2,max=3
结果为ab ac ba abc acb bac bca cba cab bc cb
C# code

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication9
{
    class Program
    {
        private int x;
        private void Permutation_Solution1(char[] pStr, int begin, int end,int min,int max)
        {
            int count = 0;
            if (begin == end - 1) //只剩一个元素
            {
                for (int i = x; i < end; i++)
                {
                    count++;
                }
                if (count >= min && count <= max)
                {
                    for (int i = x; i < end; i++) //打印
                    {
                            Console.Write(pStr[i]);
                    }
                    Console.WriteLine(string.Empty);
                }           
            }
            else
            {
                for (int k = begin; k < end; k++)
                {
                    swap(ref pStr[k], ref pStr[begin]); //交换两个字符
                    Permutation_Solution1(pStr, begin + 1, end,min,max);
                    swap(ref pStr[k], ref pStr[begin]);  //恢复
                }
            }
        }

        private void swap(ref char p1,ref char p2)
        {
            char c;
            c = p1;
            p1 = p2;
            p2 = c;
        }
        static void Main(string[] args)
        {
            string str = "abcde";
            char[] c =str.ToCharArray();
            Program g = new Program();
            int len=str.Length;
            Console.WriteLine("请输入min,max(在1和{0}之间)",len );
            int min = Convert.ToInt32(Console.ReadLine());
            int max = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < len; i++)
            {
                for (int j = 0; j < len; j++)
                {
                    g.x = i;
                    g.Permutation_Solution1(c, i, j+1,min ,max);
                }
            }
            Console.ReadKey();
        }
    }
}

再见 最后41分了 祝大家身体健康,事业有成,还有就是那个谁早点结婚吧

------解决方案--------------------
那把分先留下吧
------解决方案--------------------
you身体健康
------解决方案--------------------
LZ 还未毕业,已经有算法功底,前途不可限量。。。