計算機17-1,2作業D
阿新 • • 發佈:2018-05-08
ati tint align println apple IT col color AS
D.環形矩陣
Description |
給定一個整數m,按m形成一個環形矩陣。如m=5,則環形矩陣為: 1 1 1 1 1 1 1 1 1 |
Input |
首先是一個整數n,表示有n組數據。每組數據只一個整數m,按m形成一個環形矩陣。 |
Output |
如Script所示的環形矩陣,每個數字占4位寬度。 |
Sample Input |
3 5 3 7 |
1 import java.util.*; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 while (in.hasNext()) {7 int num = in.nextInt(); 8 while (num != 0) { 9 int n = in.nextInt(); 10 int[][] a = new int[2 * n - 1][2 * n - 1]; 11 for (int i = 1; i <= n; i++) { 12 int j; 13 for (j = i - 1; j < 2 * n - i; j++) a[i - 1][j] = i;14 for (j = i - 1; j < 2 * n - i; j++) a[2 * n - i - 1][j] = i; 15 for (j = i - 1; j < 2 * n - i; j++) a[j][i - 1] = i; 16 for (j = i - 1; j < 2 * n - i; j++) a[j][2 * n - i - 1] = i; 17 } 18 for (int i = 0; i < 2 * n - 1; i++) { 19 for (int j = 0; j < 2 * n - 1; j++) 20 System.out.print(a[i][j] + " "); 21 System.out.println(); 22 } 23 num--; 24 } 25 } 26 } 27 }
計算機17-1,2作業D