結果

問題 No.401 数字の渦巻き
ユーザー htensaihtensai
提出日時 2019-11-20 08:37:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 1,707 bytes
コンパイル時間 2,657 ms
コンパイル使用メモリ 79,236 KB
実行使用メモリ 44,948 KB
最終ジャッジ日時 2024-04-15 15:06:20
合計ジャッジ時間 8,491 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
41,616 KB
testcase_01 AC 137 ms
41,592 KB
testcase_02 AC 135 ms
41,380 KB
testcase_03 AC 134 ms
41,084 KB
testcase_04 AC 139 ms
41,228 KB
testcase_05 AC 136 ms
41,476 KB
testcase_06 AC 146 ms
41,624 KB
testcase_07 AC 147 ms
41,700 KB
testcase_08 AC 147 ms
41,488 KB
testcase_09 AC 147 ms
41,284 KB
testcase_10 AC 151 ms
41,552 KB
testcase_11 AC 153 ms
41,656 KB
testcase_12 AC 152 ms
41,436 KB
testcase_13 AC 153 ms
41,296 KB
testcase_14 AC 154 ms
41,768 KB
testcase_15 AC 160 ms
42,084 KB
testcase_16 AC 164 ms
41,780 KB
testcase_17 AC 164 ms
41,576 KB
testcase_18 AC 165 ms
41,780 KB
testcase_19 AC 183 ms
41,956 KB
testcase_20 AC 188 ms
42,496 KB
testcase_21 AC 177 ms
41,800 KB
testcase_22 AC 186 ms
42,108 KB
testcase_23 AC 181 ms
42,136 KB
testcase_24 AC 191 ms
42,124 KB
testcase_25 AC 189 ms
42,324 KB
testcase_26 AC 183 ms
41,956 KB
testcase_27 AC 190 ms
42,248 KB
testcase_28 AC 191 ms
42,208 KB
testcase_29 AC 208 ms
44,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {

	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] field = new int[n + 2][n + 2];
		for (int i = 0; i < n + 2; i++) {
		    Arrays.fill(field[i], Integer.MAX_VALUE);
		    if (i == 0 || i == n + 1) {
		        continue;
		    }
		    for (int j = 1; j <= n; j++) {
		        field[i][j] = 0;
		    }
		}
		boolean isVertical = false;
		boolean isUp = true;
		int idx = 1;
		int h = 1;
		int w = 1;
		while (idx <= n * n) {
		    field[h][w] = idx;
		    idx++;
		    int nextH;
		    int nextW;
		    if (isVertical) {
		        if (isUp) {
		            nextH = h + 1;
		        } else {
		            nextH = h - 1;
		        }
		        nextW = w;
		    } else {
		        if (isUp) {
		            nextW = w + 1;
		        } else {
		            nextW = w - 1;
		        }
		        nextH = h;
		    }
		    if (field[nextH][nextW] == 0) {
		        h = nextH;
		        w = nextW;
		        continue;
		    }
		    if (isVertical) {
		        isVertical = false;
		        isUp = !isUp;
		    } else {
		        isVertical = true;
		    }
		    if (isVertical) {
		        if (isUp) {
		            h = h + 1;
		        } else {
		            h = h - 1;
		        }
		    } else {
		        if (isUp) {
		            w = w + 1;
		        } else {
		            w = w - 1;
		        }
		    }
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= n; i++) {
		    for (int j = 1; j <= n; j++) {
		        if (j != 1) {
		            sb.append(" ");
		        }
		        sb.append(String.format("%03d", field[i][j]));
		    }
		    sb.append("\n");
		}
		System.out.print(sb);
	}
}
0