hdu1753大明A+B

大明A+B

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7385    Accepted Submission(s): 2596


Problem Description
话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。

现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。
 

Input
本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
 

Output
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
 

Sample Input
   
   
   
   
1.1 2.9 1.1111111111 2.3444323343 1 1.1
 

Sample Output
   
   
   
   
4 3.4555434454 2.1
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.StringTokenizer;
import java.io.IOException;
import java.math.BigDecimal;

class Main
{
	public static final boolean DEBUG = false;
	public StringTokenizer tokenizer;
	public PrintWriter cout;
	BufferedReader cin;
	
	
	public void init() throws IOException
	{		
		if (DEBUG) {
			cin = new BufferedReader(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new BufferedReader(new InputStreamReader(System.in));
		}
		tokenizer = new StringTokenizer("");
		
		cout = new PrintWriter(new OutputStreamWriter(System.out));	
		
	}
	
	public String next() throws IOException
	{
		while (!tokenizer.hasMoreTokens()) {
			String s = cin.readLine();
			if (s == null) return null;
			tokenizer = new StringTokenizer(s);
		}
		
		return tokenizer.nextToken();
	}
	
	public void solve(String s1, String s2)
	{
		BigDecimal bd1 = new BigDecimal(s1);
		BigDecimal bd2 = new BigDecimal(s2);
		
		BigDecimal ans = bd1.add(bd2);
		
		StringBuilder sb = new StringBuilder(ans.toPlainString());
		
		if (sb.indexOf(".") != -1) {
			for (int i = sb.length() - 1; i >= 1; i--) {
				if (sb.charAt(i) == '0') sb.deleteCharAt(i);
				else break;
			}
		}
		
		if (sb.charAt(sb.length() - 1) == '.') sb.deleteCharAt(sb.length() - 1);
		
		if (sb.indexOf(".") != -1 && sb.charAt(0) == '0') sb.deleteCharAt(0);
		
		cout.println(sb.toString());
		cout.flush();
	}
	
	public static void main(String[] args) throws IOException
	{
		Main solver = new Main();
		solver.init();
		String s;
		
		while ((s = solver.next()) != null) {
			String s1 = s;
			String s2 = solver.next();
			solver.solve(s1, s2);
		}
		
	}
}



你可能感兴趣的:(hdu1753大明A+B)