List<string> num = new List<string> {"1","2","3","4","5","6","7"};首先,任意挑选两个出来,再从剩下的挑选两个出来比如:12 - 34
12 - 35
12 - 36
12 - 37
12 - 45
12 - 46
12 - 47
12 - 56
12 - 57
12 - 67总共 21*10 = 210注
12 - 35
12 - 36
12 - 37
12 - 45
12 - 46
12 - 47
12 - 56
12 - 57
12 - 67总共 21*10 = 210注
C74 = 35C72*C52 = 210
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "1234567";
List<string> list = Combo(s, 4).SelectMany(x => Arrange(x)).ToList();
Console.WriteLine("count: " + list.Count);
list.ForEach(x => Console.WriteLine(x.Substring(0, 2) + " - " + x.Substring(2, 2)));
} static IEnumerable<string> Arrange(string source)
{
for (int i = 0; i < source.Length; i++)
{
if (source.Length == 1)
{
yield return source;
}
else
{
foreach (var x in Arrange(source.Substring(0, i) + source.Substring(i + 1)))
{
yield return source[i] + x;
}
}
}
} static IEnumerable<string> Combo(string source, int len)
{
int[] pos = new int[len];
for (int i = 0; i < len; i++) pos[i] = i;
while (pos[0] < source.Length - len)
{
string str = "";
for (int i = 0; i < len; i++) str += source[pos[i]];
for (int i = len - 1; i >= 0; i--)
{
if (pos[i] < source.Length - len + i)
{
pos[i]++;
for (int j = i + 1; j <= len - 1; j++)
{
pos[j] = pos[i] + j - i;
}
break;
}
else
{
continue;
}
}
yield return str;
}
yield return source.Substring(source.Length - len);
}
}
}
你怎么算的,一共840组。
从剩下的5个里挑选2个,C52,不对吗?C72*C52 = 210 ,不对吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "1234567";
List<string> list = Combo(s, 4).SelectMany(x => Arrange(x)).ToList();
list = list.GroupBy(x => new string(x.Substring(0, 2).OrderBy(y => y).ToArray()) + new string(x.Substring(2, 2).OrderBy(y => y).ToArray())).Select(x => x.First()).ToList();
Console.WriteLine("count: " + list.Count);
list.ForEach(x => Console.WriteLine(x.Substring(0, 2) + " - " + x.Substring(2, 2)));
} static IEnumerable<string> Arrange(string source)
{
for (int i = 0; i < source.Length; i++)
{
if (source.Length == 1)
{
yield return source;
}
else
{
foreach (var x in Arrange(source.Substring(0, i) + source.Substring(i + 1)))
{
yield return source[i] + x;
}
}
}
} static IEnumerable<string> Combo(string source, int len)
{
int[] pos = new int[len];
for (int i = 0; i < len; i++) pos[i] = i;
while (pos[0] < source.Length - len)
{
string str = "";
for (int i = 0; i < len; i++) str += source[pos[i]];
for (int i = len - 1; i >= 0; i--)
{
if (pos[i] < source.Length - len + i)
{
pos[i]++;
for (int j = i + 1; j <= len - 1; j++)
{
pos[j] = pos[i] + j - i;
}
break;
}
else
{
continue;
}
}
yield return str;
}
yield return source.Substring(source.Length - len);
}
}
}