24h購物| | PChome| 登入
2012-07-14 07:48:44| 人氣686| 回應0 | 上一篇 | 下一篇

[UVA][Java] 11821 - High-Precision Number

推薦 0 收藏 0 轉貼0 訂閱站台


Problem D

High-Precision Number
Input: Standard Input

Output: Standard Output

 

A number with 30 decimal digits of precision can be represented by a structure type as shown in the examples below. It includes a 30-element integer array (digits), a single integer (decpt) to represent the position of the decimal point and an integer (or character) to represent the sign (+/-). For example, the value -218.302869584 might be stored as

 














digits

 


 

0

 


decpt

 

3

 
 

 

 

 

 






sign

 

-1

 
 

 

 


The value 0.0000123456789 might be represented as follows.














digits

 


 

0

 


decpt

 

-4

 
 

 

 

 

 






sign

 

1

 
 

 

 


Your task is to write a program to calculate the sum of high-precision numbers.

 

Input

The first line contains a positive integer n (1≤n≤100) indicating the number of groups of high-precision numbers (maximum 30 significant digits). Each group includes high-precision numbers (one number in a line) and a line with only 0 indicating the end of each group. A group can contain 100 numbers at most.

 

Output

For each group, print out the sum of high-precision numbers (one value in a line). All zeros after the decimal point located behind the last non-zero digit must be discarded

 

 

 

 

 

 

 

 

 

 

 

Sample Input

Output for Sample Input

4
4.12345678900000000005

-0.00000000012

0

-1300.1

1300.123456789

0.0000000012345678912345

0

1500.61345975

-202.004285

-8.60917475

0

-218.302869584

200.0000123456789

0

4.12345678888000000005
0.0234567902345678912345

1290

-18.3028572383211


Problemsetter: Seksun Suwanmanee

 import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {     
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        while(t-- != 0) {
            BigDecimal sum = new BigDecimal("0");
            String next;
            while(true) {
                next = cin.next();
                if(next.compareTo("0") == 0)
                    break;
                sum = sum.add(new BigDecimal(next));
            }
            next = sum.toPlainString();
            int j = next.length()-1;
            while(next.charAt(j) == '0')    j--;
            if(next.charAt(j) == '.')    j--;
            System.out.println(next.substring(0, j+1));
        }
    }
}

台長: Morris
人氣(686) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 10509 - R U Kidding Mr. Feynman?
此分類上一篇:[UVA][D&C] 1230 - MODEX

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文