說明:大數運算,對 Java 與 Python 來說相對容易。在 Python 中使用 eval 來試試,除法中的 / 需要轉換成 // 只取商的部分。
Java 版
import java.util.Scanner;
import java.math.BigInteger;
public class A021 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
BigInteger a = new BigInteger(in.next());
String op = in.next();
BigInteger b = new BigInteger(in.next());
switch (op) {
case "*":
System.out.println(a.multiply(b));
break;
case "/":
System.out.println(a.divide(b));
break;
case "+":
System.out.println(a.add(b));
break;
default:
System.out.println(a.subtract(b));
break;
}
}
}
}
Python 版 (2022.07)while True:
try:
s = input().replace('/', '//')
r = eval(s)
print(int(r))
except:
break
沒有留言:
張貼留言