当前位置:编程学习 > C#/ASP.NET >>

不重复排序(C#)中

要求给定4个数字
用这4个数字组成各种排列大概是24种但要把这24种全部显示出来用什么方法高手指点一下
用C# --------------------编程问答-------------------- 不是高手帮你顶 --------------------编程问答-------------------- 循环向集合里加,发现重复就不加,循环完了集合里面就是结果 --------------------编程问答--------------------   
 
 static void Main(string[] args)
        {
            for (int a = 1; a <= 4; a++)
            {
                for (int b = 1; b <= 4; b++)
                {
                    for (int c = 1; c <= 4; c++)
                    {
                        for (int d = 1; d <= 4; d++)
                        {
                            if (a != b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
                            {
                                int i = a * 1000 + b * 100 + c * 10 + d;
                                Console.WriteLine(i);
                            }
                        }
                    }
                }

            }
            Console.ReadLine();
        }  --------------------编程问答-------------------- 我是个小蔡鸟,所以只会用菜鸟的方法,笨是笨了点,不过能解决你的问题~  --------------------编程问答--------------------
引用 3 楼 yzyl_213 的回复:
static void Main(string[] args)
{
for (int a = 1; a  <= 4; a++)
{
for (int b = 1; b  <= 4; b++)
{
for (int c = 1; c  <= 4; c++)
{
for (int d = 1; d  <= 4; d++)
{
if (a != b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
{
int i = a * 1000 + b * 100 + c * 10 + d;
Console.WriteLine(i);
}
}
}
}

}
Console.ReadLine();
}


顶。 --------------------编程问答-------------------- 你那是给出的四个数字为1.2.3.4时还可以了但要是给的是6,7,8,9这好像就不行了把 --------------------编程问答-------------------- 为什么不行?
只要把"1,2,3,4"改成"6,7,8,9"就可以了~
如果是不连续的数字只要去掉中间的不许要的数字就行了~ --------------------编程问答-------------------- 利用数组更好一点~
static void Main(string[] args)
        {
            int[] i = new int[4];
            Console.WriteLine("Please enter the first number:");
            i[0] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the second number:");
            i[1] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the tirnd number:");
            i[2] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the forth number:");
            i[3] = Convert.ToInt32(Console.ReadLine());
            for (int m = 0; m < 4; m++)
            {
                for (int n = 0; n < 4; n++)
                {
                    for (int t = 0; t < 4; t++)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            if (m != n && m != k && m != t && n != k && n != t && k != t)
                            {
                                int num = i[m] * 1000 + i[n] * 100 + i[t] * 10 + i[k];
                                Console.WriteLine(num);
                            }
                        }
                    }
                }
            }
            Console.ReadLine();
        }

        
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,