結果

問題 No.708 (+ー)の式
ユーザー バカらっくバカらっく
提出日時 2018-07-01 13:58:05
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 2,796 bytes
コンパイル時間 2,487 ms
コンパイル使用メモリ 106,044 KB
実行使用メモリ 23,804 KB
最終ジャッジ日時 2023-09-13 16:34:19
合計ジャッジ時間 4,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
21,700 KB
testcase_01 AC 61 ms
23,788 KB
testcase_02 AC 61 ms
21,792 KB
testcase_03 AC 61 ms
21,848 KB
testcase_04 AC 62 ms
23,544 KB
testcase_05 AC 62 ms
23,804 KB
testcase_06 AC 62 ms
21,680 KB
testcase_07 AC 61 ms
21,776 KB
testcase_08 AC 61 ms
21,708 KB
testcase_09 AC 61 ms
21,560 KB
testcase_10 AC 63 ms
21,524 KB
testcase_11 AC 63 ms
21,580 KB
testcase_12 AC 61 ms
21,636 KB
testcase_13 AC 62 ms
21,640 KB
testcase_14 AC 62 ms
21,796 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()
    {
        string inpt = Reader.ReadLine();

        string[] splt = DivideKakko(inpt);
        for (int i = 0; i < splt.Length; i++) {
            if(splt[i].Contains('(')) {
                splt[i] = GetCalc(splt[i]).ToString();                
            }
        }
        int ans = GetCalc(string.Join("", splt));
        Console.WriteLine(ans);
    }

    private int GetCalc(string inpt) {
        string src = inpt;
        if(inpt[0] == '(') {
            src = inpt.Substring(1, inpt.Length - 2);
        }
        src = src.Replace("--", "+");
        src = src.Replace("+-", "-");

        List<int> element = new List<int>();

        int idx = 0;
        for (int i = 0; i < src.Length; i++) {
            if(i>0&&src[i] == '-') {
                element.Add(int.Parse(src.Substring(idx, i - idx)));
                idx = i;
            } else if(src[i] == '+') {
                element.Add(int.Parse(src.Substring(idx, i - idx)));
                idx = i + 1;
            } else if(i == src.Length - 1) {
                element.Add(int.Parse(src.Substring(idx)));
            }
        }

        return element.Sum();
    }

    private string[] DivideKakko(String inText) {
        List<String> ret = new List<string>();
        if(!inText.Contains(')')) {
            ret.Add(inText);
            return ret.ToArray();
        }

        int stIdx = 0;
        for (int i = 0; i < inText.Length; i++) {
            if(inText[i] == '(' && i>0) {
                ret.Add(inText.Substring(stIdx, i - stIdx));
                stIdx = i;
            } else if(inText[i] == ')') {
                ret.Add(inText.Substring(stIdx, i - stIdx + 1));
                stIdx = i + 1;
            }
        }
        if(stIdx< inText.Length) {
            ret.Add(inText.Substring(stIdx, inText.Length - stIdx));
        }
        return ret.ToArray();
    }

    public class Reader
    {
        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 = @"



0+(3-6+0)+(9+5)-(5-7)+3+(8-5-2-2-6)-6+(1-2+9)+2-8-(9-3-2+4)-4-1-(1-6-2+1)







";
    }

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