C# 1到10的数组每次取三个不能重复,有多少种组合 C# 1到10的数组每次取三个不能重复,有多少种组合?{1,2,3,4,5,6,7,8,9,10}比如1,2,3 1,2,4 1,2,5 等求算法输出 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 组合问题(从M个不同字符中任取N个字符的所有组合)思路:状态数组,其下标表示1到m个数,数组元素的值为1,表示其下标代表的字符被选中,为0则没选中。 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为“01”组合,同时将其左边的所有“1”全部移动到数组的最左端。 当“1”全部移动到最右端时,就得到了最后一个组合。 public String[] GetAllCombination(String str,Int32 n) //n < str.Length { Int32[] state = new Int32[str.Length]; Char[] chars = str.ToCharArray(); List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++) { state[i] = 1; } for (Int32 i = n; i < state.Length; i++) { state[i] = 0; } list.Add(str.Substring(0,n)); while (true) { Boolean flag = true; Int32 index = 0; Int32 oneCount = 0; Int32 zeroCount = 0; StringBuilder sbuilder = new StringBuilder(); for(; index < state.Length - 1; index++) { if (state[index] == 1 && state[index + 1] == 0) { Int32 temp = state[index]; state[index] = state[index + 1]; state[index + 1] = temp; break; } } for (Int32 i = 0; i < index; i++) { if (state[i] == 0) zeroCount++; else oneCount++; } for (Int32 i = 0; i < index; i++) { if (i < oneCount) state[i] = 1; else state[i] = 0; } for (Int32 i = 0; i < chars.Length; i++) { if (state[i] == 1) sbuilder.Append(chars[i]); } list.Add(sbuilder.ToString()); for (Int32 i = 0; i < chars.Length - n; i++) { if (state[i] == 1) { flag = false; break; } } if (flag) break; } return list.ToArray(); }我的是输入一个字符串,和一个数字,比如 "abcde" 3, 然后会输出3个字母的全部组合,你稍微修改下就可以了from my bloghttp://www.cnblogs.com/Peter-Zhang/articles/1783814.html Enumeralbe.Rangevar r = from a in Enumerable.Range(1, 10) from b in Enumerable.Range(1,10) from c in Enumerable.Range(1,10)http://topic.csdn.net/u/20090217/21/F41ED9F6-F929-451C-A5C9-80D2E408422A.html class Program { static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; static void Main(string[] args) { int num = 0; string nunstr = ""; for (int i = 0; i < numlist.Length-2; i++) { for (int j = i + 1; j < numlist.Length - 1; j++) { nunstr = numlist[i] + "," + numlist[j] + "," + numlist[j + 1]; num++; Console.WriteLine(nunstr); } } Console.WriteLine(num.ToString()); } } 少了一个循环static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; static void Main(string[] args) { int num = 0; string nunstr = ""; for (int i = 0; i < numlist.Length-2; i++) { for (int j = i + 1; j < numlist.Length - 1; j++) { for (int k = i + 2; k < numlist.Length; k++) { nunstr = numlist[i] + "," + numlist[j] + "," + numlist[k]; num++; Console.WriteLine(nunstr); } } } Console.WriteLine(num.ToString()); } public static String[] GetAllCombination(Int32[] arr, Int32 n) //n < str.Length { Int32[] state = new Int32[arr.Length]; List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++) { state[i] = 1; } for (Int32 i = n; i < state.Length; i++) { state[i] = 0; } String str = ""; for(Int32 i = 0;i < n;i++) str += arr[i] + ","; list.Add(str.TrimEnd(',')); while (true) { Boolean flag = true; Int32 index = 0; Int32 oneCount = 0; Int32 zeroCount = 0; StringBuilder sbuilder = new StringBuilder(); for (; index < state.Length - 1; index++) { if (state[index] == 1 && state[index + 1] == 0) { Int32 temp = state[index]; state[index] = state[index + 1]; state[index + 1] = temp; break; } } for (Int32 i = 0; i < index; i++) { if (state[i] == 0) zeroCount++; else oneCount++; } for (Int32 i = 0; i < index; i++) { if (i < oneCount) state[i] = 1; else state[i] = 0; } for (Int32 i = 0; i < arr.Length; i++) { if (state[i] == 1) sbuilder.Append(arr[i] + ","); } list.Add(sbuilder.ToString().TrimEnd(',')); for (Int32 i = 0; i < arr.Length - n; i++) { if (state[i] == 1) { flag = false; break; } } if (flag) break; } return list.ToArray(); }这回可以了 static void Main(string[] args){ List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); StringBuilder builder = new StringBuilder(); arr.ForEach(a1 => arr.Except(new int[] { a1 }).ToList().ForEach(a2 => arr.Except(new int[] { a1, a2 }).ToList().ForEach(a3 => builder.AppendLine(a1.ToString() + "," + a2.ToString() + "," + a3.ToString())))); Console.WriteLine(builder.ToString()); File.WriteAllText("result.txt", builder.ToString()); Console.ReadKey();}运行后到你的程序目录下看result.txt文件,里面是结果。 class Program { static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; static void Main(string[] args) { int num = 0; string nunstr = ""; for (int i = 0; i < numlist.Length-2; i++) { for (int j = i + 1; j < numlist.Length - 1; j++) { for (int k = i + 2; k < numlist.Length; k++) { nunstr = numlist[i] + "," + numlist[j] + "," + numlist[k]; num++; Console.WriteLine(nunstr); } } } Console.WriteLine(num.ToString()); } } 想由哪些组成就修改这个List就可以了。List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); public static String[] GetAllCombination(Int32[] arr, Int32 n) //取n个的组合 { Int32[] state = new Int32[arr.Length]; List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++) { state[i] = 1; } for (Int32 i = n; i < state.Length; i++) { state[i] = 0; } String str = ""; for(Int32 i = 0;i < n;i++) str += arr[i] + ","; list.Add(str.TrimEnd(',')); while (true) { Boolean flag = true; Int32 index = 0; Int32 oneCount = 0; Int32 zeroCount = 0; StringBuilder sbuilder = new StringBuilder(); for (; index < state.Length - 1; index++) { if (state[index] == 1 && state[index + 1] == 0) { Int32 temp = state[index]; state[index] = state[index + 1]; state[index + 1] = temp; break; } } for (Int32 i = 0; i < index; i++) { if (state[i] == 0) zeroCount++; else oneCount++; } for (Int32 i = 0; i < index; i++) { if (i < oneCount) state[i] = 1; else state[i] = 0; } for (Int32 i = 0; i < arr.Length; i++) { if (state[i] == 1) sbuilder.Append(arr[i] + ","); } list.Add(sbuilder.ToString().TrimEnd(',')); for (Int32 i = 0; i < arr.Length - n; i++) { if (state[i] == 1) { flag = false; break; } } if (flag) break; } return list.ToArray(); } static void Main(string[] args) { Int32[] arr = { 1, 2, 3, 4, 5,6,7,8,9,10 }; String[] list = GetAllCombination(arr, 3); foreach (String s in list) Console.WriteLine(s); }修改的,取你的{1,2,3,4,5,6,7,8,9,10} 3个数的组合输出:1,2,31,2,41,3,42,3,41,2,51,3,52,3,51,4,52,4,53,4,51,2,61,3,62,3,61,4,62,4,63,4,61,5,62,5,63,5,64,5,61,2,71,3,72,3,71,4,72,4,73,4,71,5,72,5,73,5,74,5,71,6,72,6,73,6,74,6,75,6,71,2,81,3,82,3,81,4,82,4,83,4,81,5,82,5,83,5,84,5,81,6,82,6,83,6,84,6,85,6,81,7,82,7,83,7,84,7,85,7,86,7,81,2,91,3,92,3,91,4,92,4,93,4,91,5,92,5,93,5,94,5,91,6,92,6,93,6,94,6,95,6,91,7,92,7,93,7,94,7,95,7,96,7,91,8,92,8,93,8,94,8,95,8,96,8,97,8,91,2,101,3,102,3,101,4,102,4,103,4,101,5,102,5,103,5,104,5,101,6,102,6,103,6,104,6,105,6,101,7,102,7,103,7,104,7,105,7,106,7,101,8,102,8,103,8,104,8,105,8,106,8,107,8,101,9,102,9,103,9,104,9,105,9,106,9,107,9,108,9,10 如何不插入重复的数据? c#FileInfo的操作问题 RDLC如何打印若干个内存中的图片 【求救啊】数据导入搞了好多天了!没头绪,大家帮帮忙! 如何用水晶报表显示动态增加列的数据 急 *******大哥小妹帮帮我这菜鸟******* 帮忙看看下面的错误 在Web Browser中,如何实现查找功能?高手来救我! 我如何把网上的流媒体录制到本地? 请教个Soket方面的问题:(急,急急急急急急) 怎样在mobile项目中了解conpact数据库 如何格式化一个数组
首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。
然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为“01”组合,同时将其左边的所有“1”全部移动到数组的最左端。
当“1”全部移动到最右端时,就得到了最后一个组合。 public String[] GetAllCombination(String str,Int32 n) //n < str.Length
{
Int32[] state = new Int32[str.Length];
Char[] chars = str.ToCharArray();
List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++)
{
state[i] = 1;
}
for (Int32 i = n; i < state.Length; i++)
{
state[i] = 0;
} list.Add(str.Substring(0,n));
while (true)
{
Boolean flag = true;
Int32 index = 0;
Int32 oneCount = 0;
Int32 zeroCount = 0;
StringBuilder sbuilder = new StringBuilder(); for(; index < state.Length - 1; index++)
{
if (state[index] == 1 && state[index + 1] == 0)
{
Int32 temp = state[index];
state[index] = state[index + 1];
state[index + 1] = temp;
break;
}
} for (Int32 i = 0; i < index; i++)
{
if (state[i] == 0)
zeroCount++;
else
oneCount++;
} for (Int32 i = 0; i < index; i++)
{
if (i < oneCount)
state[i] = 1;
else
state[i] = 0;
} for (Int32 i = 0; i < chars.Length; i++)
{
if (state[i] == 1)
sbuilder.Append(chars[i]);
} list.Add(sbuilder.ToString()); for (Int32 i = 0; i < chars.Length - n; i++)
{
if (state[i] == 1)
{
flag = false;
break;
}
}
if (flag)
break;
} return list.ToArray();
}
我的是输入一个字符串,和一个数字,比如 "abcde" 3, 然后会输出3个字母的全部组合,你稍微修改下就可以了from my blog
http://www.cnblogs.com/Peter-Zhang/articles/1783814.html
var r = from a in Enumerable.Range(1, 10)
from b in Enumerable.Range(1,10)
from c in Enumerable.Range(1,10)
http://topic.csdn.net/u/20090217/21/F41ED9F6-F929-451C-A5C9-80D2E408422A.html
{
static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static void Main(string[] args)
{
int num = 0;
string nunstr = "";
for (int i = 0; i < numlist.Length-2; i++)
{
for (int j = i + 1; j < numlist.Length - 1; j++)
{
nunstr = numlist[i] + "," + numlist[j] + "," + numlist[j + 1];
num++;
Console.WriteLine(nunstr);
}
}
Console.WriteLine(num.ToString());
}
}
static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static void Main(string[] args)
{
int num = 0;
string nunstr = "";
for (int i = 0; i < numlist.Length-2; i++)
{
for (int j = i + 1; j < numlist.Length - 1; j++)
{
for (int k = i + 2; k < numlist.Length; k++)
{
nunstr = numlist[i] + "," + numlist[j] + "," + numlist[k];
num++;
Console.WriteLine(nunstr);
}
}
}
Console.WriteLine(num.ToString());
}
public static String[] GetAllCombination(Int32[] arr, Int32 n) //n < str.Length
{
Int32[] state = new Int32[arr.Length];
List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++)
{
state[i] = 1;
}
for (Int32 i = n; i < state.Length; i++)
{
state[i] = 0;
} String str = "";
for(Int32 i = 0;i < n;i++)
str += arr[i] + ","; list.Add(str.TrimEnd(',')); while (true)
{
Boolean flag = true;
Int32 index = 0;
Int32 oneCount = 0;
Int32 zeroCount = 0;
StringBuilder sbuilder = new StringBuilder(); for (; index < state.Length - 1; index++)
{
if (state[index] == 1 && state[index + 1] == 0)
{
Int32 temp = state[index];
state[index] = state[index + 1];
state[index + 1] = temp;
break;
}
} for (Int32 i = 0; i < index; i++)
{
if (state[i] == 0)
zeroCount++;
else
oneCount++;
} for (Int32 i = 0; i < index; i++)
{
if (i < oneCount)
state[i] = 1;
else
state[i] = 0;
} for (Int32 i = 0; i < arr.Length; i++)
{
if (state[i] == 1)
sbuilder.Append(arr[i] + ",");
} list.Add(sbuilder.ToString().TrimEnd(',')); for (Int32 i = 0; i < arr.Length - n; i++)
{
if (state[i] == 1)
{
flag = false;
break;
}
}
if (flag)
break;
} return list.ToArray();
}
这回可以了
static void Main(string[] args)
{
List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
StringBuilder builder = new StringBuilder();
arr.ForEach(a1 => arr.Except(new int[] { a1 }).ToList().ForEach(a2 => arr.Except(new int[] { a1, a2 }).ToList().ForEach(a3 => builder.AppendLine(a1.ToString() + "," + a2.ToString() + "," + a3.ToString()))));
Console.WriteLine(builder.ToString());
File.WriteAllText("result.txt", builder.ToString());
Console.ReadKey();
}运行后到你的程序目录下看result.txt文件,里面是结果。
class Program
{
static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static void Main(string[] args)
{
int num = 0;
string nunstr = "";
for (int i = 0; i < numlist.Length-2; i++)
{
for (int j = i + 1; j < numlist.Length - 1; j++)
{
for (int k = i + 2; k < numlist.Length; k++)
{
nunstr = numlist[i] + "," + numlist[j] + "," + numlist[k];
num++;
Console.WriteLine(nunstr);
}
}
}
Console.WriteLine(num.ToString());
}
}
List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
public static String[] GetAllCombination(Int32[] arr, Int32 n) //取n个的组合
{
Int32[] state = new Int32[arr.Length];
List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++)
{
state[i] = 1;
}
for (Int32 i = n; i < state.Length; i++)
{
state[i] = 0;
} String str = "";
for(Int32 i = 0;i < n;i++)
str += arr[i] + ","; list.Add(str.TrimEnd(',')); while (true)
{
Boolean flag = true;
Int32 index = 0;
Int32 oneCount = 0;
Int32 zeroCount = 0;
StringBuilder sbuilder = new StringBuilder(); for (; index < state.Length - 1; index++)
{
if (state[index] == 1 && state[index + 1] == 0)
{
Int32 temp = state[index];
state[index] = state[index + 1];
state[index + 1] = temp;
break;
}
} for (Int32 i = 0; i < index; i++)
{
if (state[i] == 0)
zeroCount++;
else
oneCount++;
} for (Int32 i = 0; i < index; i++)
{
if (i < oneCount)
state[i] = 1;
else
state[i] = 0;
} for (Int32 i = 0; i < arr.Length; i++)
{
if (state[i] == 1)
sbuilder.Append(arr[i] + ",");
} list.Add(sbuilder.ToString().TrimEnd(',')); for (Int32 i = 0; i < arr.Length - n; i++)
{
if (state[i] == 1)
{
flag = false;
break;
}
}
if (flag)
break;
} return list.ToArray();
}
static void Main(string[] args)
{
Int32[] arr = { 1, 2, 3, 4, 5,6,7,8,9,10 };
String[] list = GetAllCombination(arr, 3);
foreach (String s in list)
Console.WriteLine(s);
}
修改的,取你的{1,2,3,4,5,6,7,8,9,10} 3个数的组合输出:
1,2,3
1,2,4
1,3,4
2,3,4
1,2,5
1,3,5
2,3,5
1,4,5
2,4,5
3,4,5
1,2,6
1,3,6
2,3,6
1,4,6
2,4,6
3,4,6
1,5,6
2,5,6
3,5,6
4,5,6
1,2,7
1,3,7
2,3,7
1,4,7
2,4,7
3,4,7
1,5,7
2,5,7
3,5,7
4,5,7
1,6,7
2,6,7
3,6,7
4,6,7
5,6,7
1,2,8
1,3,8
2,3,8
1,4,8
2,4,8
3,4,8
1,5,8
2,5,8
3,5,8
4,5,8
1,6,8
2,6,8
3,6,8
4,6,8
5,6,8
1,7,8
2,7,8
3,7,8
4,7,8
5,7,8
6,7,8
1,2,9
1,3,9
2,3,9
1,4,9
2,4,9
3,4,9
1,5,9
2,5,9
3,5,9
4,5,9
1,6,9
2,6,9
3,6,9
4,6,9
5,6,9
1,7,9
2,7,9
3,7,9
4,7,9
5,7,9
6,7,9
1,8,9
2,8,9
3,8,9
4,8,9
5,8,9
6,8,9
7,8,9
1,2,10
1,3,10
2,3,10
1,4,10
2,4,10
3,4,10
1,5,10
2,5,10
3,5,10
4,5,10
1,6,10
2,6,10
3,6,10
4,6,10
5,6,10
1,7,10
2,7,10
3,7,10
4,7,10
5,7,10
6,7,10
1,8,10
2,8,10
3,8,10
4,8,10
5,8,10
6,8,10
7,8,10
1,9,10
2,9,10
3,9,10
4,9,10
5,9,10
6,9,10
7,9,10
8,9,10