題目在此,
此題筆者用Java寫,因為Java有好用的 API BigInteger 阿。
用 Python 時,可以用eval函數。
基本上此題是用陣列來處理,此部分可參考 超長整數運算(大數運算)一文。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.math.BigInteger; | |
public class JAVA { | |
public static void main(String[] args) { | |
Scanner cin = new Scanner(System.in); | |
String op; | |
BigInteger a, b, c = null; | |
while (cin.hasNext()) { | |
a = cin.nextBigInteger(); | |
op = cin.next(); | |
b = cin.nextBigInteger(); | |
if(op.equals("+") == true) | |
{ | |
c = a.add(b); | |
} | |
else if(op.equals("-") == true) | |
{ | |
c = a.subtract(b); | |
} | |
else if(op.equals("*") == true) | |
{ | |
c = a.multiply(b); | |
} | |
else if(op.equals("/") == true) | |
{ | |
c = a.divide(b); | |
} | |
System.out.println(c.toString()); | |
} | |
} | |
} |