2014-03-05 18 views
0

我想構造一個大小爲n的矩形矩陣(二維)(將由用戶輸入) 現在我想構造一個對角線模式。 例如(3×3矩陣):顯示矩陣得到一個獨特的模式

     2  3 
          1 
         4  5 

value變量將被初始化爲1,並且存儲在方陣的中心。然後value將增加並存儲在左上角,如上所示,依此類推。

這是一個簡單的程序顯示在控制檯上。 用戶輸入可以從命令行獲取。 我想概括一個for循環中的條件,它將適用於大小爲5,7,9 ...(奇數)的方陣。 對於大小爲5的矩陣將是

     6    7 
          2  3 
           1 
          4  5 
         8    9 

(空的空間可以是零)

我的代碼:

import java.util.*;  

public class MatrixAdv 
{ 
    public static void main(String args[])  
    { 
     Scanner sc=new Scanner(System.in); 

     System.out.println("Please enter size of element..."); 

     int n=sc.nextInt(); //stores size of Matrix 
     int value=0; //To be incremented everytime to get the Pattern 

     int [][] matrix = new int[n][n]; 

     int k=0; 
     for(int i=0;i<some Condition;i++) 
     { 
      for(int j=1;j<some Condition;j++) 
      {     
       k=n-2-j; 
       matrix[k][k]=++value; 
      }  
     } 
    } 

    //Display the value in matrix form 
    for(int i=0;i<n;i++) 
    { 
     for(int j=0;j<n;j++) 
     { 
      Print(matrix[i][j]+"\t"); 
     } 
     Print("\n"); 
    } 
} 
+0

問題是什麼?代碼不工作?它有一個糟糕的表現?你想知道是否有更好的方法? – Narkha

回答

1

基本上有2種選擇用於解決該:

  1. 遍歷整個矩陣,逐行,逐列檢查是否必須設置某個值t當前位置
  2. 檢查所有需要設置特定值的位置。

這兩者都與for -loop的條件無關。然而,這裏的第二種方法的例子:

public class MatrixAdv 
{ 
    public static void main(String args[]) 
    { 
     //Scanner sc = new Scanner(System.in); 
     //System.out.println("Please enter size of element..."); 
     //int n = sc.nextInt(); // stores size of Matrix 
     int n = 9; 

     int value = 0;// To be incremented everytime to get the Pattern 
     int[][] matrix = new int[n][n]; 

     matrix[n/2][n/2] = 1; 
     int maxValue = ((n/2) * 4) + 1; 
     int r = n/2 - 1; 
     int c = n/2 - 1; 
     int d = 2; 
     for (value=2; value<=maxValue; value++) 
     { 
      matrix[r][c] = value; 
      int step = ((value-2)%4); 
      switch (step) 
      { 
       case 0: c+=d; break; 
       case 1: r+=d; c-=d; break; 
       case 2: c+=d; break; 
       case 3: d+=2; r-=d-1; c-=d-1; break; 
      } 
     } 

     // Display the value in matrix form 
     for (int i = 0; i < n; i++) 
     { 
      for (int j = 0; j < n; j++) 
      { 
       if (matrix[i][j] == 0) 
       { 
        System.out.printf("%3s", "_"); 
       } 
       else 
       { 
        System.out.printf("%3d", matrix[i][j]); 
       } 
      } 
      System.out.print("\n"); 
     } 

    } 
} 
0

它非常容易打印99%的矩陣模式,如果你知道矩陣的魔術......,你可以做到這一點,而無需使用任何內存。

這裏是代碼......

#include<iostream> 
using namespace std; 
int main() 
{ 
    int n; 
    cin>>n; 
    if(n == 1) 
     cout<<1; 
    else 
    { 
     int sv=2*n-4,a,b; 
     a=sv;b=sv+1; 
     for(int i=1;i<=n;i++) 
     { 
      for(int j=1;j<=n;j++) 
      { 
       if(i == j && i+j == n+1) 
       { 
        cout<<" 1 "; 
        sv=4; 
       } 
       else 
       { 
        if(i == j) 
         cout<<" "<<a<<" "; 
        else if((i+j) == n+1) 
         cout<<" "<<b<<" "; 
        else 
         cout<<" "; 
       } 
      } 
      if(i < n/2+1) 
      { 
       sv=sv-4; 
       a=sv;b=sv+1; 
      } 
      else if(i > n/2+1) 
      { 
       sv=sv+4; 
       b=sv;a=sv+1; 
      } 
      else 
      { 
       sv=4; 
       b=sv;a=sv+1; 
      } 
      cout<<"\n"; 
     } 
    } 
}