2015-12-08 58 views
2

我是C#的新手,我想製作一個程序,需要輸入並計算出移動:1移動或2移動和這些移動的所有組合(排列)。C#排列字符串n =((n - 1)+(n - 2))

我的公式是n = ((n - 1) + (n - 2))

我有正試圖輸出結果的問題 - 我試圖通過他們,如果再else邏輯循環,但它是一個爛攤子。我不知道如何構建一個數組來支持排列。

If I move n=1 the result is 1 (1) 
If I move n=2 the results is 2 (11,2) 
If I move n=3 the results is 3 (111,12,21) 
If I move n=4 the results is 4 (1111,112,211,121,22) 

任何幫助,將不勝感激:)

謝謝!

我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 

namespace ConsoleAppFrog 
{ 
public class Program 
{ 
    public void Main(string[] args) 
    { 

     // Number of moves by using only 1 or 2 moves 

     Console.WriteLine("Enter a number of moves?"); 

     int myNumber = Convert.ToInt32(Console.ReadLine()); 
     int combenations = 0; 

     Console.WriteLine("You Chose " + myNumber + " moves :)"); 
     // Math = ((myNumber - 1) + (myNumber - 2)) = combenations 
     combenations = ((myNumber - 1) + (myNumber - 2)); 
     Console.WriteLine("There are " + combenations + " combinations of moves"); 

     // Need to output the permutations of combenations 
     var moves = ""; 

     // Gives the 1 moves    
     for (int i = 1; i <= myNumber; i++) 
     { 
      moves += "1 "; 
     } 

     Console.WriteLine(moves); 

     var moves2 = ""; 

     // Get the 2 moves and check for remainder 
     for (int i = 1; i <= (myNumber/2); i++) 
     { 
      moves2 += "2 "; 
     } 

     var moves3 = ""; 

     //need to add on 1 move if there is a remainder 
     if (myNumber % 2 != 0) 
     { 
      moves3 += "1 "; 
     } 

     Console.WriteLine(moves2 + moves3); 
     Console.WriteLine(moves3 + moves2); 
     // Stuck trying to figure out the rest of moves 

     //------------------------------------------------------------------------- 

     Console.WriteLine("Done..."); 
     Console.Read(); 

     } 
    } 
} 
+3

'N =((N - 1)+(N - 2))'不會與你的輸出感。給定'n = 1',我們有'1 =(1 - 1)+(1 - 2)',或者:'1 = 0 - 1'。 – Rob

+0

實際上你需要什麼?你給出的代碼是什麼? –

+0

在上面添加了我的代碼。需要更好的方式輸出我的動作。 –

回答

1

你可能有更好的運氣與遞歸函數。

編輯 它看起來像這樣在你的代碼

using System; 

namespace ConsoleAppFrog 
{ 
    public class Program 
    { 
     public void Main(string[] args) 
     { 
      // Number of moves by using only 1 or 2 moves 

      Console.WriteLine("Enter a number of moves?"); 

      int myNumber = int.Parse(Console.ReadLine()); 

      GenerateMoves (myNumber); 

      Console.WriteLine("Done..."); 
      Console.Read(); 
     } 

     static void GenerateMoves(int n) { 
      GenerateMoves (n, ""); 
     } 

     static void GenerateMoves(int n, string permutation) { 
      if (n==0) { System.Console.WriteLine (permutation); } 
      else { 
       if (n>=1) { GenerateMoves (n-1, permutation + "1"); } 
       if (n>=2) { GenerateMoves (n-2, permutation + "2"); } 
      } 
     } 
    } 
} 
+0

通常我會建議一個更接近OP的嘗試/思考過程的答案,但是這個解決方案非常優雅高效,我不知道如何在不遞歸的情況下解決這個問題。 – CompuChip

+0

非常感謝你:)它確實很好。我想了解靜態無效的GenerateMoves - 它是如何工作的? –

+0

我不知道我可以有效地解釋600個字符的遞歸。通過逐步調試調試器中的代碼,您可能會獲得更多好處。將「n」和「permutation」添加到「Watch」窗口並觀察每個變量在變化時的情況。將您在一張紙上觀察的內容繪製成圖形可能會有幫助。研究遞歸和堆棧幀也可能會有所幫助。 –

相關問題