|
| 1 | +package StartersSept2021; |
| 2 | + |
| 3 | +import java.io.PrintWriter; |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class D { |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + |
| 14 | + FastReader sc = new FastReader(); |
| 15 | + PrintWriter out = new PrintWriter(System.out); |
| 16 | + |
| 17 | + int t = sc.nextInt(); |
| 18 | + |
| 19 | + for (int tt = 0; tt < t; tt++) { |
| 20 | + int N = sc.nextInt(); |
| 21 | + int[][] grid = new int[N][N]; |
| 22 | + |
| 23 | + if (N % 2 == 0) { |
| 24 | + evenFill(grid); |
| 25 | + } else { |
| 26 | + oddFill(grid); |
| 27 | + } |
| 28 | + display2D(grid); |
| 29 | + } |
| 30 | + |
| 31 | + out.close(); |
| 32 | + } |
| 33 | + |
| 34 | + // use String to avoid TLE |
| 35 | + |
| 36 | + private static void oddFill(int[][] grid) { |
| 37 | + for (int i = 0; i < grid.length; i++) { |
| 38 | + Arrays.fill(grid[i], 1); |
| 39 | + grid[i][i] = -1; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static void evenFill(int[][] grid) { |
| 44 | + for (int i = 0; i < grid.length; i++) { |
| 45 | + Arrays.fill(grid[i], -1); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void display2D(int[][] grid) { |
| 50 | + for (int i = 0; i < grid.length; i++) { |
| 51 | + for (int j = 0; j < grid[i].length; j++) { |
| 52 | + System.out.print(grid[i][j] + " "); |
| 53 | + } |
| 54 | + System.out.println(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static class FastReader { |
| 59 | + BufferedReader br; |
| 60 | + StringTokenizer st; |
| 61 | + |
| 62 | + FastReader() { |
| 63 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 64 | + } |
| 65 | + |
| 66 | + String next() { |
| 67 | + while (st == null || !st.hasMoreElements()) { |
| 68 | + try { |
| 69 | + st = new StringTokenizer(br.readLine()); |
| 70 | + } catch (IOException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + return st.nextToken(); |
| 75 | + } |
| 76 | + |
| 77 | + int nextInt() { |
| 78 | + return Integer.parseInt(next()); |
| 79 | + } |
| 80 | + |
| 81 | + long nextLong() { |
| 82 | + return Long.parseLong(next()); |
| 83 | + } |
| 84 | + |
| 85 | + double nextDouble() { |
| 86 | + return Double.parseDouble(next()); |
| 87 | + } |
| 88 | + |
| 89 | + String nextLine() { |
| 90 | + String str = ""; |
| 91 | + try { |
| 92 | + str = br.readLine(); |
| 93 | + } catch (IOException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + return str; |
| 97 | + } |
| 98 | + |
| 99 | + int[] readArray(int n) { |
| 100 | + int[] temparr = new int[n]; |
| 101 | + for (int i = 0; i < n; i++) { |
| 102 | + temparr[i] = nextInt(); |
| 103 | + } |
| 104 | + return temparr; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments