結果

問題 No.708 (+ー)の式
ユーザー moriguchi1983moriguchi1983
提出日時 2018-10-30 00:15:29
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,316 bytes
コンパイル時間 840 ms
コンパイル使用メモリ 106,240 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-04-30 00:08:44
合計ジャッジ時間 1,939 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
18,176 KB
testcase_01 AC 30 ms
18,816 KB
testcase_02 AC 31 ms
18,688 KB
testcase_03 AC 28 ms
18,432 KB
testcase_04 AC 31 ms
18,688 KB
testcase_05 AC 30 ms
18,560 KB
testcase_06 AC 30 ms
18,432 KB
testcase_07 AC 30 ms
18,688 KB
testcase_08 AC 30 ms
18,688 KB
testcase_09 AC 30 ms
18,816 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 31 ms
18,816 KB
testcase_14 AC 29 ms
18,432 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //入力
            var input = Console.ReadLine();
            string tempSt = input;
            string tempNum = "";

            while (true)
            {
                //文字列中に()が有ったらそこを先に計算する
                var start = input.IndexOf("(");
                var end = input.IndexOf(")");
                if (start > -1)
                {
                    //カッコ内を抽出
                    var t = input.Substring(start + 1, ((end - 1) - start));

                    //計算
                    var cal = new Calc();
                    tempNum = cal.Decision(t);

                    if (int.Parse(tempNum) < 0)
                    {
                        input = input.Remove(start-1,1);
                    }
                        //カッコ部分を置き換え
                     input = input.Replace("(" + t + ")", tempNum);
                    
                }
                else
                {
                    break;
                }
            }
            //カッコなしで最後の計算
            var cal2 = new Calc();
            Console.WriteLine(cal2.Decision(input));
            Console.ReadKey();
        }
    }

    class Calc
    {
        private List<int> tempNum = new List<int>();
        private List<char> ope = new List<char>();

        public string Decision(string str)
        {
            foreach (var st in str)
            {
                if (st == '+' || st == '-')
                {
                    ope.Add(st);
                } else {
                    tempNum.Add(int.Parse(st.ToString()));
                }
            }

            foreach (var op in ope) {
                if(op == '+')
                {
                    tempNum[0] = tempNum[0] + tempNum[1];
                }else if(op == '-')
                {
                    var answer = tempNum[0] - tempNum[1];
                    tempNum[0] = answer;
                }
                tempNum.RemoveAt(1);
            }
            return tempNum[0].ToString();
        }
    }
}
0