結果

問題 No.401 数字の渦巻き
ユーザー atkrymatkrym
提出日時 2016-10-22 10:33:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 78,196 KB
実行使用メモリ 55,440 KB
最終ジャッジ日時 2024-05-02 22:07:56
合計ジャッジ時間 7,717 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
54,088 KB
testcase_01 AC 126 ms
54,136 KB
testcase_02 AC 105 ms
52,900 KB
testcase_03 AC 139 ms
54,052 KB
testcase_04 AC 121 ms
54,324 KB
testcase_05 AC 125 ms
54,164 KB
testcase_06 AC 126 ms
53,904 KB
testcase_07 AC 126 ms
54,076 KB
testcase_08 AC 123 ms
53,812 KB
testcase_09 AC 124 ms
54,024 KB
testcase_10 AC 128 ms
53,936 KB
testcase_11 AC 132 ms
54,312 KB
testcase_12 AC 152 ms
54,236 KB
testcase_13 AC 133 ms
54,056 KB
testcase_14 AC 142 ms
54,424 KB
testcase_15 AC 157 ms
54,284 KB
testcase_16 AC 150 ms
54,320 KB
testcase_17 AC 155 ms
54,396 KB
testcase_18 AC 143 ms
54,328 KB
testcase_19 AC 160 ms
53,932 KB
testcase_20 AC 168 ms
54,568 KB
testcase_21 AC 167 ms
54,364 KB
testcase_22 AC 179 ms
54,716 KB
testcase_23 AC 194 ms
54,220 KB
testcase_24 AC 172 ms
54,400 KB
testcase_25 AC 160 ms
54,208 KB
testcase_26 AC 172 ms
54,640 KB
testcase_27 AC 170 ms
54,520 KB
testcase_28 AC 169 ms
54,320 KB
testcase_29 AC 189 ms
55,440 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