結果
| 問題 |
No.401 数字の渦巻き
|
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2019-11-20 08:37:55 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 178 ms / 2,000 ms |
| コード長 | 1,707 bytes |
| コンパイル時間 | 1,853 ms |
| コンパイル使用メモリ | 79,124 KB |
| 実行使用メモリ | 57,188 KB |
| 最終ジャッジ日時 | 2024-10-05 13:32:57 |
| 合計ジャッジ時間 | 7,166 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] field = new int[n + 2][n + 2];
for (int i = 0; i < n + 2; i++) {
Arrays.fill(field[i], Integer.MAX_VALUE);
if (i == 0 || i == n + 1) {
continue;
}
for (int j = 1; j <= n; j++) {
field[i][j] = 0;
}
}
boolean isVertical = false;
boolean isUp = true;
int idx = 1;
int h = 1;
int w = 1;
while (idx <= n * n) {
field[h][w] = idx;
idx++;
int nextH;
int nextW;
if (isVertical) {
if (isUp) {
nextH = h + 1;
} else {
nextH = h - 1;
}
nextW = w;
} else {
if (isUp) {
nextW = w + 1;
} else {
nextW = w - 1;
}
nextH = h;
}
if (field[nextH][nextW] == 0) {
h = nextH;
w = nextW;
continue;
}
if (isVertical) {
isVertical = false;
isUp = !isUp;
} else {
isVertical = true;
}
if (isVertical) {
if (isUp) {
h = h + 1;
} else {
h = h - 1;
}
} else {
if (isUp) {
w = w + 1;
} else {
w = w - 1;
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j != 1) {
sb.append(" ");
}
sb.append(String.format("%03d", field[i][j]));
}
sb.append("\n");
}
System.out.print(sb);
}
}
htensai