62,046
社区成员
发帖
与我相关
我的任务
分享
List<string> strlist = new List<string>();
List<string> strs = "a,c,b,e,d".Split(',').OrderBy(t=>t).ToList();
string temp = "";
//5位
for (int i = 0; i<strs.Count; i++)
{
temp += strs[i] + ",";
}
strlist.Add(temp);
//4位数
for (int i = 0; i < strs.Count - 3; i++)
{
for (int j = i + 1; j < strs.Count - 2; j++)
{
for (int k = j + 1; k < strs.Count - 1; k++)
{
for (int n = k + 1; n < strs.Count; n++)
{
temp = strs[i] + "," + strs[j] + "," + strs[k] + "," + strs[n];
strlist.Add(temp);
}
}
}
}
//3位数
for (int j = 0; j < strs.Count - 2; j++)
{
for (int k = j + 1; k < strs.Count - 1; k++)
{
for (int n = k + 1; n < strs.Count; n++)
{
temp = strs[j] + "," + strs[k] + "," + strs[n];
strlist.Add(temp);
}
}
}
//2位
for (int k = 0; k < strs.Count - 1; k++)
{
for (int n = k + 1; n < strs.Count; n++)
{
temp = strs[k] + "," + strs[n];
strlist.Add(temp);
}
}
//1位
for (int n = 0; n < strs.Count; n++)
{
temp = strs[n];
strlist.Add(temp);
}
return strlist.OrderBy(t=>t).ToList();
static void Main(string[] args)
{
string metachars = "abcde";
for (int i = 1; i <= metachars.Length; i++)
{
foreach (string s in foo(metachars, i))
{
Console.WriteLine(s);
}
}
Console.ReadKey();
}
static IEnumerable<string> foo(string metachars, int place)
{
IEnumerable<string> list = metachars.Select(x => x.ToString());
List<IEnumerable<string>> source = new List<IEnumerable<string>>();
IEnumerable<string> result = new List<string>();
int index = 0;
do
{
source.Clear();
for (int i = 0; i < place; i++)
{
source.Add(list.Skip(index + i).Take(1));
}
source.Add(list.Skip(place + index));
var a = source.Aggregate((thisCurrent, nextCurrent) => (thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y))));
result = result.Concat(a);
index++;
}
while (list.Count() - place > index);
return result;
}
感觉我这个好懂点。。也好读[/quote]
好懂是相对的。
你让一个根本不会写程序的人看你觉得好懂的程序,他花1个小时未必能懂。
相反,这次你花15分钟看懂了,下次15秒钟就够了。
static void Main(string[] args)
{
string metachars = "abcde";
for (int i = 1; i <= metachars.Length; i++)
{
foreach (string s in foo(metachars, i))
{
Console.WriteLine(s);
}
}
Console.ReadKey();
}
static IEnumerable<string> foo(string metachars, int place)
{
IEnumerable<string> list = metachars.Select(x => x.ToString());
List<IEnumerable<string>> source = new List<IEnumerable<string>>();
IEnumerable<string> result = new List<string>();
int index = 0;
do
{
source.Clear();
for (int i = 0; i < place; i++)
{
source.Add(list.Skip(index + i).Take(1));
}
source.Add(list.Skip(place + index));
var a = source.Aggregate((thisCurrent, nextCurrent) => (thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y))));
result = result.Concat(a);
index++;
}
while (list.Count() - place > index);
return result;
}
感觉我这个好懂点。。也好读
static List<string> GetChildCollections2(string strTemp)
{
List<string> lstTemp = strTemp.Split(',').ToList();
return Enumerable.Range(0, 1 << lstTemp.Count).Select(x => Enumerable.Range(0, lstTemp.Count).Select(y => (x & (1 << y)) != 0 ? lstTemp[y] : null).Where(z => z != null)).Select(x => string.Join(",", x)).ToList();
}
static List<string> GetChildCollections(string strTemp)
{
List<string> lstTemp = strTemp.Split(',').ToList();
var subsets = from m in Enumerable.Range(0, 1 << lstTemp.Count)
select (from i in Enumerable.Range(0, lstTemp.Count)
where (m & (1 << i)) != 0
select lstTemp[i]).ToArray();
return subsets.Select(x => string.Join(",", x)).ToList();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string metachars = "abcde";
for (int i = 1; i <= metachars.Length; i++)
{
foreach (string s in foo(metachars, i))
{
Console.WriteLine(s);
}
}
}
static IEnumerable<string> foo(string metachars, int i)
{
var query = metachars.Select(x => x.ToString().AsEnumerable());
while (query.First().Count() < i)
query = query.SelectMany(x => metachars.Where(y => y > x.Last()).Select(y => x.Concat(y.ToString().AsEnumerable())));
return query.Select(x => string.Join(",", x));
}
}
}
a
b
c
d
e
a,b
a,c
a,d
a,e
b,c
b,d
b,e
c,d
c,e
d,e
a,b,c
a,b,d
a,b,e
a,c,d
a,c,e
a,d,e
b,c,d
b,c,e
b,d,e
c,d,e
a,b,c,d
a,b,c,e
a,b,d,e
a,c,d,e
b,c,d,e
a,b,c,d,e
请按任意键继续. . .