2009-12-25 125 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。