結果

問題 No.401 数字の渦巻き
ユーザー kano_town
提出日時 2016-07-22 22:49:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 3,780 ms
コンパイル使用メモリ 77,972 KB
実行使用メモリ 60,816 KB
最終ジャッジ日時 2024-11-06 09:09:01
合計ジャッジ時間 10,044 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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