2016-01-08 76 views
0

我有一個2D陣列(矩陣)和一個整數N,N-是例如完整的正方形java的填充2D陣列與特定的方法

如果n等於16填充矩陣是這樣的。

1 2 3 4 
12 13 14 5 
11 16 15 6 
10 9 8 7 

我該怎麼辦?

+0

,你可以寫一個循環,它填補像這樣。 – SomeJavaGuy

+0

我想算法 –

+1

https://www.quora.com/How-would-you-write-a-program-that-fills-in-a-two-dimensional-array-in-a-circular-fashion-with -the-整數-從-1對N%C2%B2 – AsSiDe

回答

0
 public static void main(String args[]) { 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("Enter the dimension of the matrix : "); 
     int dimension = Integer.parseInt(br.readLine()); 
     System.out.print("Enter the number of elements : "); 
     int n = Integer.parseInt(br.readLine()); // total number of elements to be filled 
     n = n/dimension;  // Get the number of rows and columns 
     if(n % dimension != 0){ 
      // Not a Square matrix 
     } 
     int circularArray[][] = new int[n][n]; 
     int k = 1, c1 = 0, c2 = n - 1, r1 = 0, r2 = n - 1; 

     while (k <= n * n) { 

      for (int i = c1; i <= c2; i++) { 
       circularArray[r1][i] = k++; 
      } 

      for (int j = r1 + 1; j <= r2; j++) { 
       circularArray[j][c2] = k++; 
      } 

      for (int i = c2 - 1; i >= c1; i--) { 
       circularArray[r2][i] = k++; 
      } 

      for (int j = r2 - 1; j >= r1 + 1; j--) { 
       circularArray[j][c1] = k++; 
      } 

      c1++; 
      c2--; 
      r1++; 
      r2--; 
     } 

     /* Printing the Circular matrix */ 
     System.out.println("The Circular Matrix is:"); 
     for (int i = 0; i < n; i++) { 
      for (int j = 0; j < n; j++) { 
       System.out.print(circularArray[i][j] + "\t"); 
      } 
      System.out.println(); 
     } 
    } 
0

這是一個很小的例子,它可能是這樣的。 實際發生的事情被寫爲評論。

public class TestArray { 

    // A simple enum for each direction 
    public enum Mode { 
     right, down, left, up; 
    } 

    public static void main(String[] args) { 
     final int size = 4; // We set a fixed size for the square 
     int[][] arr = new int[size][size]; // create the array from the size 
     // Running vallues 
     // i and j to refer to the array. 
     // val holds the current value to be inserted 
     // circle holds how often we are going up. Each time we go up it´s increased by one 
     // In the end this should reduce the amount of steps we do to not override 
     // already assigned values. 
     int i = 0,j = 0, val = 1, circle = 0; 
     Mode runningMode = Mode.right; // We start by going to the right. 
     while(size*size >= val) { // loop until we reached the last value 
      arr[j][i] = val++; // Assign the value and increase the value by one afterwards. 
      // We go right. 
      if(runningMode == Mode.right) { 
       // we reached the last assignable item. 
       // subtract one to not get an index out of bound, 
       // subract the variable trips that is used to get the last item for the inner circle 
       if(i==arr.length-1-circle) { 
        // We are going down now and increase j 
        runningMode = Mode.down; 
        ++j; 
       } else { 
        // go on going right. 
        ++i; 
       } 
      } else if(runningMode == Mode.down){ 
       // we reached the last assignable item. 
       // subtract one to not get an index out of bound, 
       // subract the variable trips that is used to get the last item for the inner circle 
       if(j==arr.length-1-circle) { 
        // We are going left now and decrease i 
        runningMode = Mode.left; 
        --i; 
       } else { 
        // go on going down. 
        ++j; 
       } 
      } else if(runningMode == Mode.left){ 
       // we reached the last assignable item. 
       // add the variable trips that is used to get the last item for the inner circle 
       if(i==0+circle) { 
        // We are going up now and decrease j 
        // Simultaniosly we are about to end our next circle, so we increase circle 
        runningMode = Mode.up; 
        ++circle; 
        --j; 
       } else { 
        // go on going left. 
        --i; 
       } 
      } else if(runningMode == Mode.up){ 
       // we reached the last assignable item. 
       // add the variable trips that is used to get the last item for the inner circle 
       if(j==0+circle) { 
        // We are going right now and increase i 
        runningMode = Mode.right; 
        ++i; 
       } else { 
        // go on going up. 
        --j; 
       } 
      } 
     } 
     // Print the result 
     for(int[] a : arr) { 
      for(int b : a) { 
       System.out.print(b + "\t"); 
      } 
      System.out.println(); 
     } 
    } 
}