結果
| 問題 |
No.401 数字の渦巻き
|
| コンテスト | |
| ユーザー |
YoshiRyu
|
| 提出日時 | 2016-09-01 16:02:07 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 25 ms / 2,000 ms |
| コード長 | 4,170 bytes |
| コンパイル時間 | 1,920 ms |
| コンパイル使用メモリ | 107,008 KB |
| 実行使用メモリ | 18,048 KB |
| 最終ジャッジ日時 | 2024-11-15 16:45:53 |
| 合計ジャッジ時間 | 2,660 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.IO;
public class Test
{
static int N;
static int[,] ans;
static int Row = 0;
static int Col = 0;
static int Step;
static int OutputCounter = 1;
static bool ptrSwitch = true;
public static void Solve()
{
// 【思考】
// 1からインクリメントしながら2次元配列に配置していき、最後にまとめて出力する。
// 以下の法則性に則って実装をしている。
// 「N = 4」 とした時、2次元配列の1要素をマスと考えて進む方向とその回数を観察した。
// はじめに右に4回進みんだ後、下に3回、左に3回、上に2回、右に2回、下に1回、左に1回、おわり
// とることがわかった。
// つまり、初め以降は進む方向と回数に法則性がある事がわかる。これは「N = 4」以外のケースでも同じ。
// よって方向と回数を一定パターンで繰り返すようにロジックを組んだ。
// 初期値
N = MyConsole.Int();
ans = new int[N, N];
Step = N;
// 一行目だけ先行処理
while (OutputCounter <= N)
{
ans[Row, Col++] = OutputCounter++;
}
Col--;
// 進む回数をデクリメントしながら繰り返し処理
while (Step-- > 0)
{
int cnt = 0;
if (ptrSwitch == true)
{
// ↓
for ( ; cnt < Step ; cnt++)
{
Row++;
ans[Row, Col] = OutputCounter++;
}
// ←
for (cnt = 0 ; cnt < Step ; cnt++)
{
Col--;
ans[Row, Col] = OutputCounter++;
}
}
else
{
// ↑
for ( ; cnt < Step ; cnt++)
{
Row--;
ans[Row, Col] = OutputCounter++;
}
// →
for (cnt = 0 ; cnt < Step ; cnt++)
{
Col++;
ans[Row, Col] = OutputCounter++;
}
}
// パターンフラグをひっくり返す
ptrSwitch = !ptrSwitch;
}
// 排出
for (Row = 0 ; Row < N ; Row++)
{
for (Col = 0 ; Col < N - 1 ; Col++)
Console.Write( "{0:D3} ", ans[Row, Col] ); // 半角スペース付き
Console.WriteLine( "{0:D3}", ans[Row, Col] ); // スペース付けない+改行
}
}
public static void Main()
{
MyConsole.Read(); Solve(); MyConsole.Finish();
}
}
public static class MyConsole
{
private static List<string> ReadedLine = new List<string>();
private static char[] buf = new char[1024];
private static int i;
public static void Read()
{
TextReader sr = Console.In;
int Len = sr.Read( buf, 0, 1024 );
var Line = string.Empty;
for (int i = 0 ; i < Len ; i++)
{
if (buf[i] >= 33 && buf[i] <= 126)
{
Line += buf[i];
}
else if (Line.Length > 0)
{
ReadedLine.Add( Line );
Line = string.Empty;
}
}
if (Line.Length > 0) ReadedLine.Add( Line );
Console.SetOut( new StreamWriter( Console.OpenStandardOutput() ) { AutoFlush = false } );
}
public static string String() { return ReadedLine[i++]; }
public static int Int() { return int.Parse( ReadedLine[i++] ); }
public static long Long() { return long.Parse( ReadedLine[i++] ); }
public static double Double() { return double.Parse( ReadedLine[i++] ); }
public static void wLine( string output = null )
{
Console.WriteLine( output );
}
public static void Finish()
{
Console.Out.Flush();
}
}
YoshiRyu