結果

問題 No.708 (+ー)の式
ユーザー bluemeganebluemegane
提出日時 2018-10-10 15:40:37
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 1,393 bytes
コンパイル時間 849 ms
コンパイル使用メモリ 104,448 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-04-20 19:27:19
合計ジャッジ時間 1,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
17,792 KB
testcase_01 AC 26 ms
18,048 KB
testcase_02 AC 26 ms
18,304 KB
testcase_03 AC 26 ms
18,176 KB
testcase_04 AC 26 ms
17,920 KB
testcase_05 AC 26 ms
18,304 KB
testcase_06 AC 26 ms
17,792 KB
testcase_07 AC 26 ms
17,920 KB
testcase_08 AC 26 ms
17,920 KB
testcase_09 AC 26 ms
18,048 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 26 ms
18,176 KB
testcase_14 AC 25 ms
18,176 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Collections.Generic;
using System;

public class Hello
{
    public static void Main()
    {
        var s = Console.ReadLine().Trim();
        var ans = getAns(s);
        Console.WriteLine(ans);

    }
    public static int getAns(string s)
    {
        var s2 = "";
        var p = "";
        var padd = false;
        for (int i = 0; i < s.Length; i++)
        {
            if (padd) 
            {
                if (s[i] == ')')
                {
                    s2 += calc(p).ToString();
                    padd = false;
                    p = "";
                }
                else p += s[i];
            }
            else
            {
                if (s[i] == '(') padd = true;
                else s2 += s[i];
            }
        }
        return calc(s2);
    }
    public static int calc(string s)
    {
        string[] line = s.Split('+', '-');
        var a = Array.ConvertAll(line, int.Parse);
        var aL = a.Length;
        var b = new List<int>();
        for (int i = 0; i < s.Length; i++)
            if (s[i] == '+') b.Add(1);
            else if (s[i] == '-') b.Add(0);
        int ans;
        if (b[0] == 1) ans = a[0] + a[1];
        else ans = a[0] - a[1];
        if (aL == 2) return ans;
        for (int i = 2; i < aL; i++)
            if (b[i - 1] == 1) ans += a[i];
            else ans -= a[i];
        return ans;
    }
}
0