結果
問題 | No.708 (+ー)の式 |
ユーザー |
|
提出日時 | 2018-10-31 21:17:44 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 35 ms / 2,000 ms |
コード長 | 2,407 bytes |
コンパイル時間 | 3,548 ms |
コンパイル使用メモリ | 115,268 KB |
実行使用メモリ | 27,864 KB |
最終ジャッジ日時 | 2024-11-19 13:21:11 |
合計ジャッジ時間 | 4,833 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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; } } }