读书人

求一算法.解决办法

发布时间: 2012-12-25 16:18:28 作者: rapoo

求一算法....
数据如下
一、1 2 3 44
二、a b c d
三、e f

现在要写一个循环
1ae,1af,1be,1bf...............

问题是,行数是未知的,可能是三行也可以是十行。
[解决办法]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<List<string>> list = new List<List<string>>()
{
new List<string>() { "1", "2", "3", "44" },
new List<string>() { "a", "b", "c", "d" },
new List<string>() { "e", "f" }
};
var result = list.Aggregate((current, total) => total.Join(current, (string x) => 1, (string y) => 1, (string a, string b) => b + a).ToList());
result.ForEach(x => { x.ToList().ForEach(y => Console.Write(y + " ")); Console.WriteLine(); });
}
}
}

[解决办法]
static void Main(string[] args)
{
List<string[]> list = new List<string[]>()
{
new string[]{"1","2","3","44"},
new string[]{"a","b","c","d"},
new string[]{"e","f"}

};
Fun(list, 0, "");
Console.ReadLine();
}
static void Fun(List<string[]> list, int Row, string str)
{
if (Row == list.Count)
Console.WriteLine(str);
if (Row < list.Count)
{


for (int i = 0; i < list[Row].Length; i++)
Fun(list, Row + 1, str + list[Row][i]);
}
}
[解决办法]

 List<string[]> list = new List<string[]>()
{
new string[]{"1","2","3","44"},
new string[]{"a","b","c","d"},
new string[]{"e","f"}

};
Fun(list, 0, "");
Console.ReadLine();
}
static void Fun(List<string[]> list, int Row, string str)
{
if (Row == list.Count)
Console.WriteLine(str);
if (Row < list.Count)
{
for (int i = 0; i < list[Row].Length; i++)
Fun(list, Row + 1, str + list[Row][i]);
}

读书人网 >C#

热点推荐