結果

問題 No.10 +か×か
ユーザー バカらっくバカらっく
提出日時 2017-06-28 01:03:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 112 ms / 5,000 ms
コード長 2,039 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 106,720 KB
実行使用メモリ 63,576 KB
最終ジャッジ日時 2023-08-21 06:23:01
合計ジャッジ時間 3,933 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
23,772 KB
testcase_01 AC 59 ms
21,660 KB
testcase_02 AC 58 ms
21,632 KB
testcase_03 AC 108 ms
63,504 KB
testcase_04 AC 82 ms
50,492 KB
testcase_05 AC 58 ms
21,680 KB
testcase_06 AC 112 ms
63,576 KB
testcase_07 AC 87 ms
48,544 KB
testcase_08 AC 67 ms
28,028 KB
testcase_09 AC 70 ms
35,780 KB
testcase_10 AC 59 ms
21,656 KB
testcase_11 AC 59 ms
19,756 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.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

public class Program
{

    public void Proc()
    {
        int itemCount = int.Parse(Reader.ReadLine());
        this.Total = int.Parse(Reader.ReadLine());

        this.Items = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        dic = new string[this.Items.Length][];
        string ans = GetAns(1, Items[0]);
        Console.WriteLine(ans);
    }

    private int Total;

    private int[] Items;

    private string[][] dic;
    private string GetAns(int idx, int subtotal) {
        if(subtotal > Total) {
            return string.Empty;
        }
        if(idx>=this.Items.Length-1) {
            if(subtotal+Items[idx]==Total) {
                return "+";
            }
			if (subtotal * Items[idx] == Total)
			{
				return "*";
			}
            return string.Empty;
		}

        if(dic[idx] == null) {
            dic[idx] = new string[Total + 1];
        }
        if(dic[idx][subtotal] != null) {
            return dic[idx][subtotal];
        }

        string ans = null;
        ans = GetAns(idx+1, subtotal + Items[idx]);
        if(ans.Length > 0) {
            return "+" + ans;
        }
        ans = GetAns(idx + 1, subtotal * Items[idx]);
		if (ans.Length > 0)
		{
			return "*" + ans;
		}

        dic[idx][subtotal] = string.Empty;
        return string.Empty;
	}


    public class Reader
	{
		private static StringReader sr;
		public static bool IsDebug = false;
		public static string ReadLine()
		{
			if (IsDebug)
			{
				if (sr == null)
				{
					sr = new StringReader(InputText.Trim());
				}
				return sr.ReadLine();
			}
			else
			{
				return Console.ReadLine();
			}
		}
		private static string InputText = @"


50
100000
4 6 1 8 10 2 6 4 8 9 10 8 2 1 9 5 9 1 4 1 1 6 1 9 10 9 2 7 3 8 1 4 2 7 6 10 8 7 5 5 4 2 10 8 4 8 9 5 5 9


";
	}

	public static void Main(string[] args)
	{
#if DEBUG
		Reader.IsDebug = true;
#endif
		Program prg = new Program();
		prg.Proc();
	}
}
0