結果

問題 No.401 数字の渦巻き
ユーザー Mcpu3Mcpu3
提出日時 2018-11-16 20:15:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 892 bytes
コンパイル時間 2,222 ms
コンパイル使用メモリ 73,888 KB
実行使用メモリ 62,936 KB
最終ジャッジ日時 2023-08-26 01:30:34
合計ジャッジ時間 8,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,804 KB
testcase_01 AC 126 ms
53,736 KB
testcase_02 AC 127 ms
56,048 KB
testcase_03 AC 130 ms
55,988 KB
testcase_04 AC 133 ms
55,944 KB
testcase_05 AC 133 ms
55,756 KB
testcase_06 AC 141 ms
55,660 KB
testcase_07 AC 147 ms
55,940 KB
testcase_08 AC 149 ms
55,840 KB
testcase_09 AC 150 ms
55,688 KB
testcase_10 AC 158 ms
56,180 KB
testcase_11 AC 166 ms
56,220 KB
testcase_12 AC 165 ms
56,152 KB
testcase_13 AC 180 ms
56,284 KB
testcase_14 AC 168 ms
56,344 KB
testcase_15 AC 173 ms
56,264 KB
testcase_16 AC 177 ms
55,684 KB
testcase_17 AC 194 ms
56,256 KB
testcase_18 AC 196 ms
56,392 KB
testcase_19 AC 208 ms
56,048 KB
testcase_20 AC 197 ms
55,892 KB
testcase_21 AC 200 ms
56,024 KB
testcase_22 AC 206 ms
56,296 KB
testcase_23 AC 207 ms
56,132 KB
testcase_24 AC 206 ms
56,000 KB
testcase_25 AC 223 ms
56,160 KB
testcase_26 AC 221 ms
56,276 KB
testcase_27 AC 226 ms
56,580 KB
testcase_28 AC 228 ms
56,296 KB
testcase_29 AC 254 ms
62,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), H = 0, W = 0, ans[][] = new int[N][N], tmp = 0;
		sc.close();
		for (int i = 0; i < N * N; ++i) {
			ans[H][W] = i + 1;
			if (tmp % 4 == 0) {
				if (W >= N - 1 || ans[H][W + 1] != 0) {
					++H;
					++tmp;
				}
				else ++W;
			}
			else if (tmp % 4 == 1) {
				if (H >= N - 1 || ans[H + 1][W] != 0) {
					--W;
					++tmp;
				}
				else ++H;
			}
			else if (tmp % 4 == 2) {
				if (W <= 0 || ans[H][W - 1] != 0) {
					--H;
					++tmp;
				}
				else --W;
			}
			else {
				if (H <= 0 || ans[H - 1][W] != 0) {
					++W;
					++tmp;
				}
				else --H;
			}
		}
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) System.out.printf("%03d ", ans[i][j]);
			System.out.println();
		}
	}
}
0