結果

問題 No.401 数字の渦巻き
ユーザー Mcpu3
提出日時 2018-11-16 20:15:44
言語 Java
(openjdk 23)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 892 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 77,128 KB
実行使用メモリ 43,332 KB
最終ジャッジ日時 2024-12-24 14:15:33
合計ジャッジ時間 8,614 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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