问题描述
List<string> list = new List<string>();
list.Add("A");
list.Add("B");
List<string> list1 = new List<string>();
list.Add("a");
list.Add("b");
for (int i = 0; i < list.Count; i++)
{
// print another list items.
for (int j = 0; j < list1.Count; j++)
{
Console.WriteLine("/" + list[i] + "/" + list1[j]);
}
}
我想像这样编码 string tmpS = + list [i];
加入下一个列表项目togeter。
I want to code like this string tmpS =+ list[i];
to Join the next list item togeter.
然后打印$ c> tmpS
then print tmpS
但编译错误CS0023:运算符'+'不能应用于类型为'string'的操作数。
but compile error CS0023: Operator '+' cannot be applied to operand of type 'string'.
如何打印下列所有项目(任何类型都可以)
How to print all the items below.(any sort is ok)
A
Aa
Ab
Aab
Aba
AB
ABa
ABb
ABab
ABba
B
Ba
Bb
Bab
Bba
(上限号码无交换小字符应该被转换,并且始终遵循大写字母附加小字符。)
(The Caps number No swap. the small characters should be swaped. and always follow Caps Numbers Append small characters.)
推荐答案
它使我很久没有在纯粹的算法问题!
it makes a long time I did not worked on a pure algorithmic problem!
这个程序应该诀窍:
class Program
{
static void Main(string[] args)
{
List<string> uppers = new List<string>();
uppers.Add("A");
uppers.Add("B");
List<string> lowers = new List<string>();
lowers.Add("a");
lowers.Add("b");
List<string> combinedUppers = GetCombinedItems(uppers);
List<string> combinedLowers = GetCombinedItems(lowers);
List<string> combinedUppersLowers = GetCombinedList(combinedUppers, combinedLowers);
foreach (string combo in combinedUppersLowers)
{
Console.WriteLine(combo);
}
Console.Read();
}
static private List<string> GetCombinedItems(List<string> list)
{
List<string> combinedItems = new List<string>();
for (int i = 0; i < list.Count; i++)
{
combinedItems.Add(list[i]);
for (int j = 0; j < list.Count; j++)
{
if (list[i] != list[j])
{
combinedItems.Add(String.Format("{0}{1}", list[i], list[j]));
}
}
}
return combinedItems;
}
static private List<string> GetCombinedList(List<string> list1, List<string> list2)
{
List<string> combinedList = new List<string>();
for (int i = 0; i < list1.Count; i++)
{
combinedList.Add(list1[i]);
for (int j = 0; j < list2.Count; j++)
{
combinedList.Add(String.Format("{0}{1}", list1[i], list2[j]));
}
}
for (int i = 0; i < list2.Count; i++)
{
combinedList.Add(list2[i]);
for (int j = 0; j < list1.Count; j++)
{
combinedList.Add(String.Format("{0}{1}", list2[i], list1[j]));
}
}
return combinedList;
}
}
问候。
此程序为您提供以下输出:
This program gives you this output:
这篇关于打印列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!