Editorial for 加加減減


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: WillyPillow

Subtask 1

去掉結尾的 =。(WA 20%)

Subtask 1~4

Solution 1

直接模擬即可。(AC 100%)

Solution 2

另個作法是使用某些語言的 eval。(AC 100%)

標程

Python3 eval(by WillyPillow
print(eval(input()[:-1]))
Scala + JSR-223 eval Javascript(by WillyPillow
object x001 extends App {
  val e = new javax.script.ScriptEngineManager().getEngineByExtension("js")
  println(e.eval(scala.io.StdIn.readLine().dropRight(1)))
}
C++ 模擬 (by tan7680
#include<bits/stdc++.h>
using namespace std;
int main()
{
    cin.tie(0);ios_base::sync_with_stdio(false);
    int ans,x;
    char c;
    cin>>ans;
    while(cin>>c&&c!='=')
    {
        cin>>x;
        if(c=='+')ans+=x;
        else if(c=='-')ans-=x;
    }
    cout<<ans<<endl;
return 0;
}

Comments

There are no comments at the moment.