A组有2个数(0,1),B组有2个数(4,5)
任意两个搭配,一共有4种搭配法,
如果A组有2个数(0,1),B组有2个数(4,5)、C组有2个数字(7,8)
任意三个搭配,一共有8种搭配法,现在告诉你有N组,每组里面用一个数与其他组搭配,对于每次搭配而言,每个组都只出一个数字,
求出所有的组合,放到一个集合中,(只求组合,不求排列)请写出算法。

解决方案 »

  1.   

    最简单的就是for嵌套,也可以用递归算法。
      

  2.   

    这个有点麻烦  你可以遍历你的数字
    比如
    02,03,12,13,43
    先遍历每个数字的第一位,重复的就放在一组A(0,1),不重复的单独(4)
    在遍历每个数字的第一位,重复的就放在另一组B(2,3),不重复的单独(没有)
    得出的结果就是
    A(0,1)B(2,3)  还有个单独的4
      

  3.   

    “A组有2个数(0,1),B组有2个数(4,5),任意两个搭配,一共有4种搭配法...”
    这不是标准的组合吧,是不是有什么限制,如果是标准的组合那么,有n种组合,其中:
    n=4!/(2!*2!)=6种,而不是你说的4种。
    如果是标准的组合算法,那很简单。递归首选。
      

  4.   

    一个比较笨的办法,不知道适不适合你 
     DataTable OrAry = new DataTable();//创建用于存储所有原始数据
                OrAry.Columns.Add("data1", typeof(int));//第一列数据
                OrAry.Columns.Add("data2", typeof(int));//第二列数据            DataTable newAry = new DataTable();//创建用于存储组合后的数据
                newAry.Columns.Add("data1", typeof(int));
                newAry.Columns.Add("data2", typeof(int));             int aryCount = OrAry.Rows.Count;//计算有多少对原始数据
                 int sum = int.Parse(Math.Pow(2, aryCount).ToString());
                  for (int i = 0; i < aryCount; i++)//对原始数据进行每一行循环
                  {
                      int tmpdata1 = int.Parse(OrAry.Rows[i][0].ToString());//该行第一列数据
                      int tmpdata2 = int.Parse(OrAry.Rows[i][1].ToString());//该行第二列数据
                    for (int j = 0; j < aryCount; j++)//开始循环与其他数据组合
                    {
                        if (j != i)//不与自己组合,每次组合产生4组数据存储在newAry中(一共4*(aryCount-1)个)
                        {
                            DataRow tpDr = newAry.NewRow();
                            tpDr["data1"] = tmpdata1;
                            tpDr["data2"] = OrAry.Rows[j][0];                        DataRow tpDr1 = newAry.NewRow();
                            tpDr1["data1"] = tmpdata1;
                            tpDr1["data2"] = OrAry.Rows[j][1];                        newAry.Rows.Add(tpDr);
                            newAry.Rows.Add(tpDr1);                        DataRow tpDr3 = newAry.NewRow();
                            tpDr3["data1"] = tmpdata2;
                            tpDr3["data2"] = OrAry.Rows[j][0];                        DataRow tpDr4 = newAry.NewRow();
                            tpDr4["data1"] = tmpdata2;
                            tpDr4["data2"] = OrAry.Rows[j][1];                        newAry.Rows.Add(tpDr3);
                            newAry.Rows.Add(tpDr4);
                            newAry.AcceptChanges();                    }
                        else
                            continue;                }//循环完成所有组合就在newAry中
      

  5.   

    http://topic.csdn.net/u/20110606/13/b41f8542-9996-4494-9bf2-313ac784a44b.html
      

  6.   

            static void Main(string[] args)
            {
                int[,] intarr = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
                int[,] newarr = Combo(intarr);
                Console.ReadKey();
            }        private static int[,] Combo(int[,] intarr)
            {
                int x = 2;
                int y = intarr.Length / x;
                int xy = (int)Math.Pow(x, y);
                int[,] result = new int[xy, y];
                for (int i = 0; i < xy; i++)
                {
                    for (int j = 0; j < y; j++)
                    {
                        result[i, j] = intarr[j, i % (xy / (int)Math.Pow(x, j)) / (xy / (int)Math.Pow(x, j + 1))];
                        Console.Write(result[i, j] + " ");
                    }
                    Console.WriteLine();
                }
                return result;
            }
    /*
    输出:
    1 3 5 7
    1 3 5 8
    1 3 6 7
    1 3 6 8
    1 4 5 7
    1 4 5 8
    1 4 6 7
    1 4 6 8
    2 3 5 7
    2 3 5 8
    2 3 6 7
    2 3 6 8
    2 4 5 7
    2 4 5 8
    2 4 6 7
    2 4 6 8
    */
      

  7.   

                int[][] arr = { new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { 5, 6 } };
                int[] temp = new int[arr.Length];
                int Count = arr.Length;
                int index = Count - 1;
                while (index >= 0)
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        Console.Write(arr[i][temp[i]]);
                    }
                    Console.WriteLine();
                    index = Count - 1;
                    while (index >= 0 && ++temp[index] == arr[index].Length)
                    {
                        temp[index] = 0;
                        index--;
                    }
                }
      

  8.   

    http://baike.baidu.com/view/79382.htmlz自己看百度百科怎么说滴至于c#版笛卡尔积的代码到处都是滴,记得本版精华贴里就有一份
      

  9.   

    http://topic.csdn.net/u/20110610/13/d1924231-0a45-4826-ac2a-7bbcfcb9fc93.html
      

  10.   

    http://topic.csdn.net/u/20111023/10/3eb292fd-72cb-44c6-b664-f0d074b7c44e.html两个本质上一样的问题,我在这个帖子 构造了2个 A-Z的数组,然后求笛卡尔积
      

  11.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        static class Enumerable
        {
            public static IEnumerable<IEnumerable<TSource>> Combo<TSource>(this IEnumerable<IEnumerable<TSource>> source, IEnumerable<TSource> second)
            {
                foreach (var i in second)
                {
                    foreach (var j in source)
                    {
                        List<TSource> list = new List<TSource>();
                        list.Add(i);
                        yield return j.Concat(list);
                    }
                }
            }        public static IEnumerable<IEnumerable<TSource>> StartCombo<TSource>(this IEnumerable<TSource> first)
            {
                foreach (var i in first)
                {
                    List<TSource> list = new List<TSource>();
                    list.Add(i);
                    yield return list;
                }
            }
        }    class Program
        {
            static void Main(string[] args)
            {
                List<List<string>> list = new List<List<string>>()
                {
                    new List<string>() { "0", "1" }, //a
                    new List<string>() { "4", "5" }, //b
                    new List<string>() { "7", "8" } //c,如果是N组,用循环添加到list里面
                };
                var result = list.Skip(1).Aggregate(list.First().StartCombo(), (serials, current) => serials.Combo(current), x => x).ToList();
                result.ForEach(x => { x.ToList().ForEach(y => Console.Write(y + " ")); Console.WriteLine(); });
            }
        }
    }