結果

問題 No.401 数字の渦巻き
ユーザー kano_townkano_town
提出日時 2016-07-22 22:49:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 2,913 ms
コンパイル使用メモリ 78,228 KB
実行使用メモリ 48,384 KB
最終ジャッジ日時 2024-04-24 02:25:52
合計ジャッジ時間 8,517 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
40,392 KB
testcase_01 AC 110 ms
41,308 KB
testcase_02 AC 107 ms
41,344 KB
testcase_03 AC 117 ms
41,180 KB
testcase_04 AC 113 ms
40,880 KB
testcase_05 AC 111 ms
40,744 KB
testcase_06 AC 120 ms
40,360 KB
testcase_07 AC 129 ms
41,472 KB
testcase_08 AC 132 ms
41,636 KB
testcase_09 AC 150 ms
42,260 KB
testcase_10 AC 150 ms
41,720 KB
testcase_11 AC 143 ms
42,356 KB
testcase_12 AC 148 ms
42,416 KB
testcase_13 AC 147 ms
41,652 KB
testcase_14 AC 147 ms
41,900 KB
testcase_15 AC 169 ms
41,652 KB
testcase_16 AC 167 ms
41,640 KB
testcase_17 AC 175 ms
42,696 KB
testcase_18 AC 172 ms
42,096 KB
testcase_19 AC 188 ms
42,848 KB
testcase_20 AC 184 ms
41,956 KB
testcase_21 AC 188 ms
41,764 KB
testcase_22 AC 200 ms
42,636 KB
testcase_23 AC 191 ms
41,932 KB
testcase_24 AC 187 ms
42,232 KB
testcase_25 AC 195 ms
42,168 KB
testcase_26 AC 197 ms
42,944 KB
testcase_27 AC 192 ms
42,304 KB
testcase_28 AC 199 ms
42,808 KB
testcase_29 AC 236 ms
48,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Yukicoder401 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), cnt = 1;
        int[] res = new int[n * n];
        Arrays.fill(res, 0);
        int x = 0, y = 0, dx = 1, dy = 0;
        while (cnt <= n * n) {
                res[y * n + x] = cnt++;
                if ((x + dx) < 0 || (x + dx) > n - 1 || (y + dy) < 0 || (y + dy) > n - 1 || res[(y + dy) * n + (x + dx)] != 0) {
                    if (dx == 1) {
                        dx = 0;
                        dy = 1;
                    } else if (dy == 1) {
                        dx = -1;
                        dy = 0;
                    } else if (dx == -1) {
                        dx = 0;
                        dy = -1;
                    } else if (dy == -1) {
                        dx = 1;
                        dy = 0;
                    }
                }
                x += dx;
                y += dy;
        }
        for (y = 0; y < n; y++) {
            for (x = 0; x < n; x++) {
                System.out.printf("%03d ", res[y * n + x]);
            }
            System.out.println("");
        }
    }
}
0