結果

問題 No.401 数字の渦巻き
ユーザー atkrymatkrym
提出日時 2016-10-22 10:33:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 74,608 KB
実行使用メモリ 60,116 KB
最終ジャッジ日時 2023-08-15 10:34:11
合計ジャッジ時間 7,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
55,856 KB
testcase_01 AC 111 ms
56,144 KB
testcase_02 AC 112 ms
55,908 KB
testcase_03 AC 116 ms
56,300 KB
testcase_04 AC 117 ms
55,840 KB
testcase_05 AC 116 ms
55,564 KB
testcase_06 AC 122 ms
55,848 KB
testcase_07 AC 126 ms
56,448 KB
testcase_08 AC 131 ms
55,884 KB
testcase_09 AC 128 ms
56,008 KB
testcase_10 AC 128 ms
56,012 KB
testcase_11 AC 129 ms
55,860 KB
testcase_12 AC 129 ms
55,852 KB
testcase_13 AC 130 ms
55,988 KB
testcase_14 AC 134 ms
55,700 KB
testcase_15 AC 148 ms
56,236 KB
testcase_16 AC 139 ms
55,956 KB
testcase_17 AC 161 ms
55,896 KB
testcase_18 AC 143 ms
56,436 KB
testcase_19 AC 166 ms
56,132 KB
testcase_20 AC 164 ms
55,864 KB
testcase_21 AC 154 ms
56,152 KB
testcase_22 AC 164 ms
56,232 KB
testcase_23 AC 161 ms
56,528 KB
testcase_24 AC 168 ms
56,052 KB
testcase_25 AC 164 ms
56,160 KB
testcase_26 AC 155 ms
56,356 KB
testcase_27 AC 163 ms
55,988 KB
testcase_28 AC 170 ms
56,016 KB
testcase_29 AC 187 ms
60,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    private static int[][] ary;
    public static void main(String[] args) throws Exception {
        int n = sc.nextInt();
        move(n);
        show(n);
    }
    
    public static void move(int n) {
        ary = new int[n][n];
        int x = 0;
        int y = 0;
        int[] dx = {0, 1, 0, -1};
        int[] dy = {1, 0, -1, 0};
        int d = 0;
        for (int i = 1;i <= n*n;i++) {
            ary[x][y] = i;
            int nx = x + dx[d];
            int ny = y + dy[d];
            if (nx < 0 || n <= nx || ny < 0 || n <= ny) {
                d = (d+1)%4;
                nx = x + dx[d];
                ny = y + dy[d];
            } else if (ary[nx][ny] > 0) {
                d = (d+1)%4;
                nx = x + dx[d];
                ny = y + dy[d];
            }
            x = nx;
            y = ny;
        }
    }
    
    public static void show(int n) {
        for (int i = 0;i < n;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < n;j++) {
                sb.append(String.format("%03d", ary[i][j])).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
}
0