递归    class Program
    {
        static void Main(string[] args)
        {
            string[] arrIn = new string[7];
            for (int i = 0; i < arrIn.Length; i++)
            {
                arrIn[i] = (i + 1).ToString();
            }
            PutN(arrIn, 5);
            Console.WriteLine("处理结束");
            Console.ReadLine();
        }        /// <summary>
        /// 输出N个数
        /// </summary>
        /// <param name="arrIn">输入的数组</param>
        /// <param name="N">N个一组</param>
        private static void PutN(string[] arrIn, int N)
        {
            List<string> lstUse = new List<string>();
            List<string> lstLeft = new List<string>(arrIn);
            Loop(lstUse, lstLeft, N);
        }        /// <summary>
        /// 递归函数
        /// </summary>
        /// <param name="lstUse">已选用的数据</param>
        /// <param name="lstLeft">剩余数据</param>
        /// <param name="N">N个一组</param>
        private static void Loop(List<string> lstUse, List<string> lstLeft, int N)
        {
            if (lstUse.Count + lstLeft.Count > N)
            {
                if (lstUse.Count < N)
                {
                    List<string> lstUseTmp1 = new List<string>(lstUse);
                    List<string> lstUseTmp2 = new List<string>(lstUse);
                    lstUseTmp1.Add(lstLeft[0]);
                    List<string> lstLeftTmp = new List<string>(lstLeft);
                    lstLeftTmp.RemoveAt(0);                    //处理下一个数
                    //情况1:选用下一个数
                    Loop(lstUseTmp1, lstLeftTmp, N);
                    //情况2:不选用下一个数
                    Loop(lstUseTmp2, lstLeftTmp, N);
                }
                else if (lstUse.Count == N)
                {
                    Console.WriteLine(string.Join("", lstUse));
                }
            }
            else if (lstUse.Count + lstLeft.Count == N)
            {
                Console.WriteLine(string.Join("", lstUse.Concat(lstLeft)));
            }
        }
    }

解决方案 »

  1.   

    这就是数学里面的组合,有公式,直接用就行了。假设数组升序为n, n>=5. 要抽取5个元素。则组合的情况为:
    n! / ((n-5)!*5!)
      

  2.   

    http://bbs.csdn.net/topics/390550326?page=1#post-395315609
      

  3.   

    在c#中可以这样写using System;
    using System.Collections.Generic;
    using System.Linq;namespace ConsoleApplication1
    {
        class Program
        {
            private static IEnumerable<List<int>> 组合(List<int> arr, int count, int num)
            {
                if (num <= 0)
                    yield break;
                else if (count == num)
                    yield return arr.Take(count).ToList();
                else
                {
                    foreach (var x in 组合(arr, count - 1, num))
                        yield return x;                foreach (var x in 组合(arr, count - 1, num - 1))
                    {
                        x.Add(arr[count - 1]);
                        yield return x;
                    }
                }
            }        static void Main(string[] args)
            {
                var arr = new List<int> { 1, 2, 3, 4, 5, 8 };
                foreach (var x in 组合(arr, arr.Count, 5 ))
                    Console.WriteLine(string.Join(",", x.Select(y => y.ToString())));
                Console.ReadKey();
            }
        }
    }在javascript中,你需要自己实现迭代器(自定义类型),然后就可以了。