首页 > 其他 > 详细

A+B Problem IV

时间:2014-11-30 21:20:21      阅读:316      评论:0      收藏:0      [点我收藏+]

描述acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这个问题给解决了。

 
输入
包含多组测试数据
每组数据包含两个正数A,B(可能为小数且位数不大于400)
输出
每组输出数据占一行,输出A+B的结果,结果需要是最简的形式。
样例输入
1.9 0.1
0.1 0.9
1.23 2.1
3 4.0
样例输出
2
1
3.33
7

 1 import java.math.BigDecimal;
 2 import java.math.BigInteger;
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         Scanner scanner=new Scanner(System.in);
 8         BigDecimal a;
 9         BigDecimal b;
10         String s;
11         String words[];
12         
13         while(scanner.hasNext()){
14             a=scanner.nextBigDecimal();
15             b=scanner.nextBigDecimal();
16             s=a.add(b).toString();
17             words=s.split("\\.");
18             System.out.print(words[0]);
19             
20             if(s.contains(".")){
21                 words[1]=new StringBuffer(words[1]).reverse().toString();
22                 words[1]=words[1].replaceAll("^(0*)","");
23                 words[1]=new StringBuffer(words[1]).reverse().toString();
24                 
25                 if(words[1].compareTo("")!=0)
26                     System.out.print("."+words[1]);
27             }
28             System.out.println();
29         }
30         
31     } 
32 }

 

 

A+B Problem IV

原文:http://www.cnblogs.com/zqxLonely/p/4133620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!