日期:2014-05-18  浏览次数:20771 次

求教:输入一个4位数怎么把他排成24个不同的数组输出
谁能给我给“输入4位数,输出24组位置不同的的数组”的方法啊!!!!!
谢谢了!

------解决方案--------------------
啥叫做位置不同?
------解决方案--------------------
感觉用排列组合的思想去写算法吧。
第一次从4个数中取一个,然后从剩下的3个数中取一个,接着在剩下的两个数中取一个。
重复这种步骤4次
------解决方案--------------------
参考,LINQ的写法
http://topic.csdn.net/u/20120407/20/197e6e0b-1f24-4537-a8e7-00feeae85a52.html?66150
------解决方案--------------------
探讨
参考,LINQ的写法
http://topic.csdn.net/u/20120407/20/197e6e0b-1f24-4537-a8e7-00feeae85a52.html?66150

------解决方案--------------------
代码替你粘过来

C# code

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            排列组合("ABC", 0, 3).ToList().ForEach(x => { Console.WriteLine(x); });
            Console.ReadKey();
        }

        private static IEnumerable<string> 排列组合(string source, int fr, int len)
        {
            if (len == 1)
                return new List<string> { source.Substring(fr, 1) };
            else
                return from sub in 排列组合(source, fr + 1, len - 1)
                       from i in Enumerable.Range(0, sub.Length + 1)
                       let first = source.Substring(fr, 1)
                       select sub.Insert(i, first);
        }
    }
}

------解决方案--------------------
这个问题有人问过类似的好像