結果

問題 No.401 数字の渦巻き
ユーザー YamaKasaYamaKasa
提出日時 2019-02-03 19:09:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 1,515 bytes
コンパイル時間 4,470 ms
コンパイル使用メモリ 75,436 KB
実行使用メモリ 63,064 KB
最終ジャッジ日時 2023-08-22 22:15:44
合計ジャッジ時間 9,562 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
56,036 KB
testcase_01 AC 130 ms
55,720 KB
testcase_02 AC 131 ms
55,732 KB
testcase_03 AC 131 ms
56,296 KB
testcase_04 AC 135 ms
55,736 KB
testcase_05 AC 138 ms
55,772 KB
testcase_06 AC 142 ms
55,424 KB
testcase_07 AC 144 ms
55,900 KB
testcase_08 AC 148 ms
55,696 KB
testcase_09 AC 150 ms
55,988 KB
testcase_10 AC 152 ms
55,984 KB
testcase_11 AC 161 ms
55,544 KB
testcase_12 AC 156 ms
56,140 KB
testcase_13 AC 159 ms
55,648 KB
testcase_14 AC 163 ms
56,004 KB
testcase_15 AC 176 ms
56,024 KB
testcase_16 AC 179 ms
55,716 KB
testcase_17 AC 176 ms
57,916 KB
testcase_18 AC 177 ms
55,972 KB
testcase_19 AC 186 ms
55,972 KB
testcase_20 AC 186 ms
56,324 KB
testcase_21 AC 188 ms
56,620 KB
testcase_22 AC 189 ms
56,200 KB
testcase_23 AC 200 ms
56,448 KB
testcase_24 AC 208 ms
56,360 KB
testcase_25 AC 204 ms
56,560 KB
testcase_26 AC 217 ms
56,256 KB
testcase_27 AC 217 ms
56,732 KB
testcase_28 AC 223 ms
56,640 KB
testcase_29 AC 259 ms
63,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		String[][] s = new String[n][n];
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				s[i][j] = "";
			}
		}
		int x = 0;
		int y = 0;
		int k = 2;
		int t = 0;
		s[y][x] = String.format("%03d", 1);
		int cnt = 0;
		while(k <= n * n) {
//			cnt++;
//			System.out.println(y + " " + x);
//			System.out.println(t);
			if(t == 0) {
				if(x + 1 < n) {
					if(s[y][x + 1].equals("")) {
						s[y][x + 1] = String.format("%03d", k);
						k++;
						x++;
					}else {
						t = 1;
					}
				}else {
					t = 1;
				}
			}else if(t == 1) {
				if(y + 1 < n) {
					if(s[y + 1][x].equals("")) {
						s[y + 1][x] = String.format("%03d", k);
						k++;
						y++;
					}else {
						t = 2;
					}
				}else {
					t = 2;
				}
			}else if(t == 2) {
				if(x - 1 >= 0) {
					if(s[y][x - 1].equals("")) {
						s[y][x - 1] = String.format("%03d", k);
						k++;
						x--;
					}else {
						t = 3;
					}
				}else {
					t = 3;
				}
			}else if(t == 3) {
				if(y - 1 >= 0) {
					if(s[y - 1][x].equals("")) {
						s[y - 1][x] = String.format("%03d", k);
						k++;
						y--;
					}else {
						t = 0;
					}
				}else {
					t = 0;
				}
			}
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(j == n - 1) {
					System.out.println(s[i][j]);
				}else {
					System.out.print(s[i][j] +" ");
				}
			}
		}
	}
}
0