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 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num = in.nextInt(); while (num != 0) { int n = in.nextInt(); int[][] a = new int[2 * n - 1][2 * n - 1]; for (int i = 1; i <= n; i++) { int j; for (j = i - 1; j < 2 * n - i; j++) a[i - 1][j] = i; for (j = i - 1; j < 2 * n - i; j++) a[2 * n - i - 1][j] = i; for (j = i - 1; j < 2 * n - i; j++) a[j][i - 1] = i; for (j = i - 1; j < 2 * n - i; j++) a[j][2 * n - i - 1] = i; } for (int i = 0; i < 2 * n - 1; i++) { for (int j = 0; j < 2 * n - 1; j++) System.out.print(a[i][j] + " "); System.out.println(); } num--; } } } }