您的位置:首页 > 其它

UVa10464 - Big Big Real Numbers

2014-05-09 23:35 363 查看
Problem B:Big Big Real Numbers
Time Limit: 1 second

Memory Limit: 32 MB
Aztec warriors are very good at math (Sssh! It�s a secret information :). As an Aztec, little Ahuitzotl has his heart�s wish to be a great warrior. But he is not good at math. There is a tradition in Aztecs that
foundation of being very good at math is to know how to add really big decimal point numbers. With this wish in his heart, little Ahuitzotl started to practice adding such big real numbers. He wants your help to become very good at math. He wants you to write
a program for him so that when he adds two decimal numbers he can match his results with your programs output. You see, you have such a big responsibility (to help a kid being a great warrior :)

Input

First line of the input is a non negative integer N. Next N line follows a pair of real numbers separated by a space in each line. Negative real number can appear in the input. Length of each number can be 1000
digits both before and after decimal point. The Input ends at EOF. You can assume that no invalid number will be given as input.

Output

You have to print the result after adding every pair of numbers in different lines. There should be at least one digit after and before the decimal point and no trailing and leading zeros should be printed.

Sample Input

8
1111.332 1123.1112
.223 9.8963
0.002331 .0012
1111.20000 1.0000
004112.000 21.00
.123 .001
3.333 -1.111
-1.111 3.333

Sample Output

2234.4432
10.1193
0.003531
1112.2
4133.0
0.124
2.222
2.222

import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
import java.math.BigDecimal;

class Main
{
public static final boolean DEBUG = false;
public BufferedReader cin;
public PrintWriter cout;
public StringTokenizer tokenizer;

public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}

cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StringTokenizer("");

} catch (Exception e) {
e.printStackTrace();
}
}

public String next()
{
while (!tokenizer.hasMoreTokens()) {
try {
String s = cin.readLine();
if (s == null) return null;
tokenizer = new StringTokenizer(s);
} catch (Exception e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}

public String input()
{
return next();
}

public void solve(String a, String b)
{
BigDecimal biga = new BigDecimal(a);
BigDecimal bigb = new BigDecimal(b);

String answer = biga.add(bigb).toPlainString();

StringBuilder sb = new StringBuilder(answer);
if (sb.indexOf(".") == -1) {
sb.append(".0");
} else {
for (int i = sb.length() - 1, end = sb.indexOf(".") + 1; i > end; i--) {
if (sb.charAt(i) == '0') sb.deleteCharAt(i);
else break;
}
}
cout.println(sb.toString());
cout.flush();
}

public static void main(String[] args)
{
Main solution = new Main();
solution.init();

int t = Integer.parseInt(solution.input());

while (t-- > 0) {
String a = solution.next();
String b = solution.next();
solution.solve(a, b);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: