A组有2个数(0,1),B组有2个数(4,5)
任意两个搭配,一共有4种搭配法,
如果A组有2个数(0,1),B组有2个数(4,5)、C组有2个数字(7,8)
任意三个搭配,一共有8种搭配法,现在告诉你有N组,每组里面用一个数与其他组搭配,对于每次搭配而言,每个组都只出一个数字,
求出所有的组合,放到一个集合中,(只求组合,不求排列)请写出算法。
任意两个搭配,一共有4种搭配法,
如果A组有2个数(0,1),B组有2个数(4,5)、C组有2个数字(7,8)
任意三个搭配,一共有8种搭配法,现在告诉你有N组,每组里面用一个数与其他组搭配,对于每次搭配而言,每个组都只出一个数字,
求出所有的组合,放到一个集合中,(只求组合,不求排列)请写出算法。
解决方案 »
- this.Refresh()问题
- datatable中如何根据条件批量更新某个字段的值
- 【在线等求解】dataGridView从数据库读表 判断如果课程等于1 Cells[4] 显示语文 出错…… 提示输入字符不正确
- Label字体大小
- C#解决方案里文件夹的GUID
- 如何方便的编辑代码,谢谢
- 求一个日期的写法
- 提一个百分问题 windows forms的 包括思路 就指望这个养家糊口了 请大家倾囊相授!
- 醒目:如果这都不算倒粪,那什么算呢????????????????????????????????????
- 如何网页中添加按钮?
- (高手帮助!)c# 让picturebox真正透明
- winform做的一个程序在XP系统下没问题,到win7上就有问题了?
比如
02,03,12,13,43
先遍历每个数字的第一位,重复的就放在一组A(0,1),不重复的单独(4)
在遍历每个数字的第一位,重复的就放在另一组B(2,3),不重复的单独(没有)
得出的结果就是
A(0,1)B(2,3) 还有个单独的4
这不是标准的组合吧,是不是有什么限制,如果是标准的组合那么,有n种组合,其中:
n=4!/(2!*2!)=6种,而不是你说的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中
{
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
*/
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--;
}
}
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(); });
}
}
}