2017-09-07 143 views
0

作爲一個賦值,我必須創建一個螺旋矩陣,其中用戶輸入行數和列數。 這是我的代碼爲遠(第一年的大學學業,所以不要對我太狠心)輸入行和列的螺旋矩陣

Console.Write("Enter n: "); 
     int n = int.Parse(Console.ReadLine()); 
     int[,] matrix = new int[n, n]; 
     int row = 0; 
     int col = 0; 
     string direction = "right"; 
     int maxRotations = n * n; 

     for (int i = 1; i <= maxRotations; i++) 
     { 
      if (direction == "right" && (col > n - 1 || matrix[row, col] != 0)) 
      { 
       direction = "down"; 
       col--; 
       row++; 
      } 
      if (direction == "down" && (row > n - 1 || matrix[row, col] != 0)) 
      { 
       direction = "left"; 
       row--; 
       col--; 
      } 
      if (direction == "left" && (col < 0 || matrix[row, col] != 0)) 
      { 
       direction = "up"; 
       col++; 
       row--; 
      } 

      if (direction == "up" && row < 0 || matrix[row, col] != 0) 
      { 
       direction = "right"; 
       row++; 
       col++; 
      } 

      matrix[row, col] = i; 

      if (direction == "right") 
      { 
       col++; 
      } 
      if (direction == "down") 
      { 
       row++; 
      } 
      if (direction == "left") 
      { 
       col--; 
      } 
      if (direction == "up") 
      { 
       row--; 
      } 
     } 

      // display matrica 

     for (int r = 0; r < n; r++) 
     { 
      for (int c = 0; c < n; c++) 
      { 
       Console.Write("{0,4}", matrix[r, c]); 

      } 
      Console.WriteLine(); 

     } 
     Console.ReadLine(); 

我有點失落,如何幹這事知道如何循環與基體行和列的數字相同,但它應該是非方形矩陣。

4×3矩陣

8 9 10 1 
7 12 11 2 
6 5 4 3 

5×2矩陣

3 4 
12 5 
11 6 
10 7 
9 8 
+0

一些幫助我仍然在這裏尋找一些幫助...... –

回答

0

這裏是我想出了一個解決方案 - 從這裏的社區:)

Console.Write("Enter n: "); 
     int n = int.Parse(Console.ReadLine()); 
     Console.Write("Enter m: "); 
     int m = int.Parse(Console.ReadLine()); 
     int[,] matrix = new int[n,m]; 
     int row = 0; 
     int col = 0; 
     string direction = "right"; 
     int maxRotations = n * m; 

     for (int i = 1; i <= maxRotations; i++) 
     { 
      if (direction == "right" && (col > m - 1 || matrix[row, col] != 0)) 
      { 
       direction = "down"; 
       col--; 
       row++; 
      } 
      if (direction == "down" && (row > n - 1 || matrix[row, col] != 0)) 
      { 
       direction = "left"; 
       row--; 
       col--; 
      } 
      if (direction == "left" && (col < 0 || matrix[row, col] != 0)) 
      { 
       direction = "up"; 
       col++; 
       row--; 
      } 

      if (direction == "up" && row < 0 || matrix[row, col] != 0) 
      { 
       direction = "right"; 
       row++; 
       col++; 
      } 

      matrix[row, col] = i; 

      if (direction == "right") 
      { 
       col++; 
      } 
      if (direction == "down") 
      { 
       row++; 
      } 
      if (direction == "left") 
      { 
       col--; 
      } 
      if (direction == "up") 
      { 
       row--; 
      } 
     } 

     // displej matrica 

     for (int r = 0; r < n; r++) 
     { 
      for (int c = 0; c < m ; c++) 
      { 
       Console.Write("{0,4}", matrix[r,c]); 
      } 
      Console.WriteLine(); 

     } 
     Console.ReadLine(); 
    }