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 |
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 1000digits 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); } } }
相关文章推荐
- 开车路上怎样赶走瞌睡虫?网友支招如何防路困
- JavaScript实现动态广告弹出框
- 256 <--> 64算法 互转 ( Java实现)
- excel快递单号查询工具以及源码
- 刹车八个技巧 教你踩得又稳又好
- 正强学习笔记之JDBC基础知识
- 利用ffmpeg框架扩展android平台解码器
- 1Z0-051 QUESTION 14 DISTINCT的用法
- Constructor does Real Work
- 纪录片
- Linux驱动修炼之道-INPUT子系统(上)
- NET简单的一个画图程序
- Query on a tree 树链剖分 第一次写
- 我的Fedora环境
- CMD命令进入D盘
- 图像处理学习之路
- 编译apache报APR not found
- 云计算之路-阿里云上:读取缓存时的“黑色0.1秒”
- Tomcat一些小事
- 开车人千金难买的知识!(组图)