2009-12-25 90 views
7
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。打印列表项目

然后打印tmpS

但编译错误CS0023:运算符“+”不能被应用于类型“字符串”的操作数。

如何打印下面的所有项目。(任何种类是确定)

一个 机管局 抗体 AAB 阿坝 AB 阿坝州 ABB ABAB ABBA 乙 巴 了Bb Bab Bba

(The Caps number No swap。小字符应该交换。并始终按照Caps Numbers追加小字符。)

+0

你确定Aab必须在AB部分? – bniwredyc 2009-12-25 08:27:56

+0

你好。没有区别不同。只需要上面的所有组合。列表组合他的项目并将其项目与其他列表项目组合。 – 2009-12-25 08:34:46

回答

3

它使我很长一段时间没有在纯算法问题上工作!

这个程序应该做的伎俩:

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; 
    } 
} 

问候。


这个节目给你这样的输出:

一个 机管局 AAB 抗体 阿坝 AB 阿坝州 ABAB ABB ABBA 乙 巴 巴布 了Bb BBA BA BAa BAAB BAB BABA 一个 AA αAB内 AB ABA AB ABA ABAB ABB ABBA b BA BAB BB BBA BA BAA BAAB BAB 巴巴

+0

@TheRHCP。我为List1和List2添加了更多的itmes,当我为List1添加A,B,C时; a,b,c代表List2;脚本无法Pirnt超过4个字符(如ABCABC/ACadc/ABCab ......不能打印。我想要做的是结合了列表1和列表2所有的条件是什么)。目前只支持NewString <= 4个字符。 – 2009-12-26 15:58:16

+0

算法我都给你无法通过列表处理超过2个caracters。为了解决这个问题,你必须调整GetCombinedItems方法。我认为它可以很容易地adpated但要实现该算法是一个比较复杂的设计,因为它必须处理数目不详的caracters的。事实上,此方法只产生caracters的所有可能的组合的名单,我认为你可以找到这种algortihm在互联网上。 – Ucodia 2009-12-27 12:20:23

3

这听起来像是功课。

List<string> list = new List<string>();  
list.Add("A"); 
list.Add("B"); 

List<string> list1 = new List<string>();  
list.Add("a"); 
list.Add("b"); 

string xxx = ""; 
for (int i = 0; i < list.Count; i++) 
{ 
    xxx += list[i]; 
    Console.WriteLine(xxx); 

    // print another list items. 
    for (int j = 0; j < list1.Count; j++) 
    { 
     Console.WriteLine("/" + list[i] + "/" + list1[j]); 
    } 

} 
+0

你好Slugster,请您指导。我发现一些项目仍然无法打印。 – 2009-12-25 10:52:33

3

+=没有=+

但是你应该使用StringBuilder。