UVA11821 High-Precision Number大数

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11821 High-Precision Number大数相关的知识,希望对你有一定的参考价值。

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
在这里插入图片描述

    The value 0.0000123456789 might be represented as follows.
在这里插入图片描述

    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 highprecision 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
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
Sample Output
4.12345678888000000005
0.0234567902345678912345
1290
-18.3028572383211

问题链接UVA11821 High-Precision Number
问题简述:(略)
问题分析:大数问题,用Java语言来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的Java语言程序如下:

/* UVA11821 High-Precision Number */

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

public class Main {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        BigDecimal res = BigDecimal.ZERO;
        BigDecimal num = BigDecimal.ZERO;
        while(t-- != 0) {
            res = BigDecimal.ZERO;
            num = BigDecimal.ZERO;
            do{
                res = res.add(num);
                num = input.nextBigDecimal();
            } while(num.compareTo(BigDecimal.ZERO) != 0);
            char output[] = res.toString().toCharArray();
            int len = output.length-1;
            while(len >= 0 && output[len]=='0')
                len--;
            if (len >= 0 && output[len]=='.')
                len--;
            for (int i = 0;i <= len;i++)
                System.out.print(output[i]);
            System.out.println();
        }
    }
}

以上是关于UVA11821 High-Precision Number大数的主要内容,如果未能解决你的问题,请参考以下文章

题解 UVA1185 Big Number

uva 10870

UVA 12050

UVA725

题解 UVa13244

UVa10025 The ? 1 ? 2 ? ... ? n = k problem