結果

問題 No.10 +か×か
ユーザー AreTrashAreTrash
提出日時 2016-04-13 15:21:04
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,487 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 112,736 KB
実行使用メモリ 28,760 KB
最終ジャッジ日時 2024-04-15 00:43:10
合計ジャッジ時間 7,431 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
24,764 KB
testcase_01 AC 27 ms
24,844 KB
testcase_02 AC 28 ms
25,140 KB
testcase_03 AC 45 ms
24,972 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Linq;

namespace No10_1{
    public class Program{
        public static int n = Read(Convert.ToInt32);
        public static int total = Read(Convert.ToInt32);
        public static List<int> an = ReadList(Convert.ToInt32);
        public static Stack<bool> stk = new Stack<bool>(); 
        public static void Main(string[] args){
            an.Insert(0, 0);
            Solve(true, 0, 0);

            stk.Pop();
            stk.Pop();

            var result = "";
            foreach(var s in stk){
                result += s ? "+" : "*";
            }
            Console.WriteLine(result);
        }
        public static bool Solve(bool op, int sum, int cnt){
            if(sum == total && cnt == n + 1) return true;
            if(sum > total || cnt > n) return false;

            if(Solve(true, sum + an[cnt], cnt + 1)){
                stk.Push(true);
                return true;
            }
            if(Solve(false, sum * an[cnt], cnt + 1)) {
                stk.Push(false);
                return true;
            }
            return false;
        }

        public static TOutput Read<TOutput>(Converter<string, TOutput> converter){
            return converter(Console.ReadLine());
        }

        public static List<TOutput> ReadList<TOutput>(Converter<string, TOutput> converter, char c = ' '){
            return Console.ReadLine()?.Split(c).ToList().ConvertAll(converter);
        }
    }
}
0