UVa10464 - Big Big Real Numbers

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);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值