Exact Sum of Real Numbers
Time limit1sMemory limit128 MB
Add each group of decimal numbers with up to 30 digits exactly and print the unrounded sum with trailing zeros removed.
- Level
Medium4 of 10
- Topics
- Implementation, String, Math
- Solved
- No attempts yet
Problem
Sanggeun stores a real number in a struct. The struct has an integer array digits that holds 30 digits, an integer decpt that records where the decimal point sits, and a sign field that records + or -. This scheme keeps a value such as -218.302869584 or 0.0000123456789 exactly, with no rounding.
Given several real numbers, write a program that adds them Sanggeun's way and produces the exact sum.
Input
The first line has the number of test cases. Each test case is a list of real numbers, and a single 0 at the end marks where that test case stops. The terminating 0 is not added into the sum.
Every real number in the input has at most 30 digits, and one test case holds at most 100 real numbers.
Output
For each test case print the sum of its real numbers on one line. Never round. Write the sum exactly, stop at the last nonzero digit, and drop the zeros that follow it.
The format obeys these rules.
- When no fractional digit remains, leave out the decimal point too. A sum of 1290 is written 1290.
- When the absolute value is smaller than 1, write exactly one 0 in front of the decimal point. Write 0.2, not .2.
- Do not pad the integer part with leading zeros.
- Write a leading - only when the sum is negative.
- When the sum is 0, print 0.