Nan*_* HE 8 c# data-structures
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
我想像这样编码string tmpS =+ list[i];加入下一个列表项togeter.
然后打印 tmpS
但编译错误CS0023:运算符'+'不能应用于'string'类型的操作数.
如何打印下面的所有项目.(任何种类都可以)
A A Ab Aab Aba AB ABa ABb ABab ABba B Ba Bb Bab Bba
(大写号码没有交换.小字符应该被交换.并且始终跟随大写号码附加小字符.)
这让我很长一段时间没有研究纯算法问题了!
这个程序应该可以解决这个问题:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
问候。
该程序为您提供以下输出:
A Aa Aab Ab Aba AB ABa ABab ABb ABba B Ba Bab Bb Bba BA BAa BAab Bab BAba a aA aAB aB aBA ab abA abAB abB abBA b BA bAB bB bBA ba baA baAB baB baBA