結果

問題 No.401 数字の渦巻き
ユーザー YamaKasa
提出日時 2019-02-03 19:09:34
言語 Java
(openjdk 23)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 1,515 bytes
コンパイル時間 2,371 ms
コンパイル使用メモリ 79,588 KB
実行使用メモリ 60,440 KB
最終ジャッジ日時 2024-12-18 00:50:50
合計ジャッジ時間 9,035 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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