24h購物| | PChome| 登入
2012-07-18 13:40:24| 人氣1,369| 回應1 | 上一篇 | 下一篇

[UVA][Java] 288 - Arithmetic Operations With Large Integers

推薦 0 收藏 0 轉貼0 訂閱站台


 Arithmetic Operations With Large Integers 

This problem is about calculations with large numbers. Large means numbers with at most one thousand digits. The operations are limited to addition, substraction, multiplication and raising to a higher power.

There are no limitations to the operants of addition, substraction and multiplication. The base in raising to higher powers is positive and smaller than ten. The exponent is positive.

The inputfile consists of a valid expression with any number of operations. There are no parentheses, but the normal arithmetic priority rules are still valid.

An example of a valid expression is: 12345678 * 129876 + 2**1993. An invalid expression is: 12345678 * 129876 + 12**1993 because the base is greater than nine.

Input

The input contains several test cases, each one on a different line. Each test case contains numbers and operands in the following way: n op n { op n } . n is a positive decimal number with at most one thousand digits, stored as an ASCII-text. op is one of the following: +, -, *, ** (** means ``raising to higher powers''). There can be at most one hundred operations per test case. There are no spaces or other illegal characters in the input.

Output

The output contains the exact result of the evaluated expressions given in the input. Print each test case in a different line. Each test case won't have more than three thousand characters.

Sample Input

12345678*129876+2**1993

Sample Output

896977105683011347056900938420064050017435704756793125373158388145129891712\\
789307700515223684770523373785909874208955291755561688174261977676508872005\\
197801086953040197752187505381087095625350558038492109870986287356370809737\\
409093338414265941143390397695285610643740694879918793932122262001282984143\\
224073001319601441082075018589725061828585163552941409601583724270514300953\\
188533095947591884905338415676554651534516617357655143781579373852994152663\\
198702360093129335607684294312805938140290754926427776409574872859496315224\\
893901812925850900592061583009183090068756428459147015355107518672556877720




import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String line;
while(cin.hasNextLine()) {
line = cin.nextLine();
BigInteger[] S = new BigInteger[201];
int[] neg = new int[200];
int Idx = 0, len = line.length();
int oper = 0;
for(int i = 0; i < len; i++) {
String sub = "";
while(i < len && Character.isDigit(line.charAt(i))) {
sub += line.charAt(i);
i++;
}
if(oper == 0) { // add
S[Idx] = new BigInteger(sub);
neg[Idx] = 0;
Idx++;
} else if(oper == 1) {// subtract
S[Idx] = new BigInteger(sub);
neg[Idx] = 1;
Idx++;
} else if(oper == 2) {// multiply
S[Idx-1] = S[Idx-1].multiply(new BigInteger(sub));
} else {
if(S[Idx-1].longValue() < 2)
continue;
S[Idx-1] = S[Idx-1].pow(Integer.parseInt(sub));
}
if(i < len && line.charAt(i) == '+') {
oper = 0;
} else if(i < len && line.charAt(i) == '-') {
oper = 1;
} else if(i < len && line.charAt(i) == '*') {
if(i+1 < len && line.charAt(i+1) == '*') {
i++;
oper = 3;
} else {
oper = 2;
}
}
}
BigInteger ans = new BigInteger("0");
for(int i = 0; i < Idx; i++) {
if(neg[i] == 1)
ans = ans.subtract(S[i]);
else
ans = ans.add(S[i]);
}
System.out.println(ans);
}
}
}


台長: Morris
人氣(1,369) | 回應(1)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 255 - Correct Move
此分類上一篇:[UVA] 296 - Safebreaker

小嫩嫩
請問這題如果用C++寫要怎麼做呢
還有判斷先乘除 指數後加減的問題在上面的code是在哪個部分
2014-10-30 12:16:30
版主回應
如果使用 C++,你必須了解如何實作 D&C 的次方運算,例如快速計算 50^50 等。而先乘除後加減,就好像是你把所有乘除法法都算完留下加減。

例如 1 * 2 - 3 * 4 => 最後加總 2 - 12。
讀入的過程中
=> 1
=> 2
=> 2 -3
=> 2 -12
最後加總所有數字即可。如果遇到乘除法,依附在前一個數字即可,反之斷開成為一個新的數字。
2014-11-01 18:18:54
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文