結果

問題 No.708 (+ー)の式
ユーザー moriguchi1983moriguchi1983
提出日時 2018-10-31 21:17:44
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 2,407 bytes
コンパイル時間 2,440 ms
コンパイル使用メモリ 106,644 KB
実行使用メモリ 24,148 KB
最終ジャッジ日時 2023-08-12 12:19:20
合計ジャッジ時間 4,754 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
22,080 KB
testcase_01 AC 81 ms
22,036 KB
testcase_02 AC 80 ms
22,160 KB
testcase_03 AC 79 ms
22,040 KB
testcase_04 AC 83 ms
24,128 KB
testcase_05 AC 83 ms
20,184 KB
testcase_06 AC 83 ms
24,088 KB
testcase_07 AC 82 ms
24,148 KB
testcase_08 AC 81 ms
24,132 KB
testcase_09 AC 82 ms
22,048 KB
testcase_10 AC 84 ms
22,052 KB
testcase_11 AC 83 ms
22,168 KB
testcase_12 AC 83 ms
22,048 KB
testcase_13 AC 81 ms
22,140 KB
testcase_14 AC 79 ms
24,060 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));

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

                    //カッコ部分を置き換え
                    input = input.Replace("(" + t + ")", tempNum);
                    input = Calc.NewMethod(input);
                }
                else
                {
                    break;
                }
            }
            //カッコなしで最後の計算
            input = Calc.NewMethod(input);
            Console.WriteLine(Calc.Decision(input));
            Console.ReadKey();
        }
    }

    class Calc
    {
        public static string Decision(string str)
        {
            var ope = new List<char>();
            var opeSt = new[] {'+','-'};
            var tempNum = Array.ConvertAll(str.Split(opeSt), s => int.Parse(s)).ToList();

            foreach (var st in str)
            {
                if (st == '+' || st == '-') ope.Add(st);
            }
            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();
        }

        public static string NewMethod(string input)
        {
            input = input.Replace("++", "+");
            input = input.Replace("--", "+");
            input = input.Replace("+-", "-");
            input = input.Replace("-+", "-");
            return input;
        }
    }
}
0