結果

問題 No.2595 Parsing Challenge
ユーザー ei1333333
提出日時 2023-12-23 01:07:17
言語 Go
(1.23.4)
結果
WA  
実行時間 -
コード長 773 bytes
コンパイル時間 16,203 ms
コンパイル使用メモリ 221,188 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-27 11:51:15
合計ジャッジ時間 18,006 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 15 WA * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"go/token"
	"go/types"
	"os"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	stdin.Scan()
	exp := stdin.Text()
	exp = simplifyExpression(exp)
	res, _ := types.Eval(token.NewFileSet(), nil, token.NoPos, exp)
	fmt.Println(res.Value)
}

func simplifyExpression(exp string) string {
	var result strings.Builder
	prevChar := ' '
	positive := true
	for _, char := range exp {
		if char == '+' || char == '-' {
			if char == '-' {
				positive = !positive
			}
		} else {
			if prevChar == '+' || prevChar == '-' {
				if positive {
					result.WriteRune('+')
				} else {
					result.WriteRune('-')
				}
			}
			result.WriteRune(char)
			positive = true
		}

		prevChar = char
	}

	return result.String()
}
0