結果

問題 No.401 数字の渦巻き
ユーザー ぴろず
提出日時 2016-07-22 22:26:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 684 bytes
コンパイル時間 3,050 ms
コンパイル使用メモリ 78,016 KB
実行使用メモリ 60,016 KB
最終ジャッジ日時 2024-11-06 08:57:31
合計ジャッジ時間 9,445 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

package no402;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] a = new int[n][n];
		int di = 0, dj = 1;
		int ci = 0, cj = 0;
		for(int i=1;i<=n*n;i++) {
			a[ci][cj] = i;
			int ni = ci + di;
			int nj = cj + dj;
			if (ni < 0 || ni >= n || nj < 0 || nj >= n || a[ni][nj] != 0) {
				int temp = di;
				di = dj;
				dj = -temp;
			}
			ci = ci + di;
			cj = cj + dj;
		}
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if (j > 0) {
					System.out.print(' ');
				}
				System.out.print(String.format("%03d", a[i][j]));
			}
			System.out.println();
		}
	}

}
0