結果

問題 No.401 数字の渦巻き
ユーザー YoshiRyuYoshiRyu
提出日時 2016-09-01 15:50:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 3,282 bytes
コンパイル時間 812 ms
コンパイル使用メモリ 111,956 KB
実行使用メモリ 26,096 KB
最終ジャッジ日時 2024-04-27 14:12:05
合計ジャッジ時間 2,420 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
25,712 KB
testcase_01 AC 22 ms
23,652 KB
testcase_02 AC 21 ms
23,652 KB
testcase_03 AC 21 ms
23,944 KB
testcase_04 AC 21 ms
25,708 KB
testcase_05 AC 22 ms
23,908 KB
testcase_06 AC 21 ms
23,908 KB
testcase_07 AC 22 ms
25,980 KB
testcase_08 AC 22 ms
23,796 KB
testcase_09 AC 22 ms
23,936 KB
testcase_10 AC 22 ms
25,948 KB
testcase_11 AC 21 ms
25,980 KB
testcase_12 AC 21 ms
25,720 KB
testcase_13 AC 21 ms
23,644 KB
testcase_14 AC 21 ms
23,988 KB
testcase_15 AC 22 ms
25,816 KB
testcase_16 AC 21 ms
23,688 KB
testcase_17 AC 21 ms
23,728 KB
testcase_18 AC 23 ms
23,984 KB
testcase_19 AC 22 ms
23,984 KB
testcase_20 AC 23 ms
25,856 KB
testcase_21 AC 22 ms
23,904 KB
testcase_22 AC 22 ms
26,096 KB
testcase_23 AC 21 ms
23,908 KB
testcase_24 AC 22 ms
23,648 KB
testcase_25 AC 22 ms
25,856 KB
testcase_26 AC 23 ms
26,076 KB
testcase_27 AC 23 ms
25,816 KB
testcase_28 AC 22 ms
25,948 KB
testcase_29 AC 22 ms
25,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.IO;
 
public class Test
{
    public static void Solve()
    {
        int N = MyConsole.Int();

        int[,] ans = new int[N, N];

        int Row = 0;
        int Col = 0;
        int Step = N;
        int OutputCounter = 1;

        // 一行目だけ先行処理
        while (OutputCounter <= N)
        {
            ans[Row, Col] = OutputCounter++;
            Col++;
        }

        Col--;

        bool ptrSwitch = true;

        // 進む回数をデクリメントしながら繰り返し処理
        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();
    }
}
0