結果

問題 No.10 +か×か
ユーザー rtomprtomp
提出日時 2016-10-31 16:39:21
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 3,121 bytes
コンパイル時間 3,220 ms
コンパイル使用メモリ 108,208 KB
実行使用メモリ 22,468 KB
最終ジャッジ日時 2023-08-16 11:41:16
合計ジャッジ時間 5,746 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
using System.Linq;

public class YukiCoder
{
    private static int N;
    private static int Total;
    private static int[] A;

    private static string Anser = string.Empty;

    public static void Solve()
    {
        N = MyConsole.Int();
        Total = MyConsole.Int();
        A = MyConsole.IntSplit();

        // 演算開始 戻り値は受け取らない
        Math( "", A[0], 1 );

        MyConsole.wLine( Anser );
    }

    /// <summary>
    /// 合計に達しない限り+と×の演算を行う。
    /// +と×の組み合わせ総当りで式を作って演算を行っている。
    /// </summary>
    /// <param name="Formula">計算式</param>
    /// <param name="Base">経過値</param>
    /// <param name="Index">位置情報</param>
    /// <returns>終わっている?[true:終わった false:まだまだ!] </returns>
    private static bool Math( string Formula, int Base, int Index )
    {
        if (Base == Total && Index == N)
        {
            Anser = Formula;
            return true;
        }
        else if (Base > Total || Index >= N)
        {
            return false;
        }

        if (Math( Formula + "+", Base + A[Index], Index + 1 ) == true) return true;
        if (Math( Formula + "*", Base * A[Index], Index + 1 ) == true) return true;

        return false;
    }

    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] >= 32 && 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 string[] StringSplit() { return ReadedLine[i++].Split( new[] { ' ', '^' } ); }
    public static int Int() { return int.Parse( ReadedLine[i++] ); }
    public static int[] IntSplit()
    {
        string[] strs = StringSplit();
        int[] ret = new int[strs.Length];
        for (int i = 0 ; i < strs.Length ; i++)
        {
            ret[i] = int.Parse( strs[i] );
        }

        return ret;
    }
    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