結果

問題 No.708 (+ー)の式
ユーザー バカらっく
提出日時 2018-07-01 13:58:05
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 2,796 bytes
コンパイル時間 952 ms
コンパイル使用メモリ 113,324 KB
実行使用メモリ 27,124 KB
最終ジャッジ日時 2024-07-01 01:09:50
合計ジャッジ時間 1,949 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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.

ソースコード

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();
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0