Page 1 of 1

11821 - High-Precision Number

Posted: Thu Aug 21, 2014 7:26 pm
by RookiE3
Can anybody please tell me what am I doing wrong here:

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class Main {

    static BigDecimal num, sum;
    static String line;

    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(input.readLine());
        while (t > 0) {
            sum = BigDecimal.ZERO;
            while (input.ready()) {
                line = input.readLine();
                if(line.length() == 1 && line.charAt(0) == '0') {
                    break;
                }
                num = new BigDecimal(line);
                sum = sum.add(num);
            }
            t--;
            System.out.println(sum.stripTrailingZeros().toPlainString());
        }
    }
}

Re: 11821 - High-Precision Number

Posted: Tue Aug 26, 2014 6:36 pm
by lighted
Change line

Code: Select all

while (input.ready()) {
    line = input.readLine();
It must be

Code: Select all

while ( (line = input.readLine()) != null ) {
Change line to

Code: Select all

num = new BigDecimal(line.trim());
If you have problems with Java post in Help on languages ‹ Java.

Re: 11821 - High-Precision Number

Posted: Tue Aug 26, 2014 6:55 pm
by RookiE3
I have changed the code as you said, but still WA :(

Please tell me if logic is OK or not.

Re: 11821 - High-Precision Number

Posted: Thu Aug 28, 2014 12:01 am
by brianfry713
Input:

Code: Select all

1
0.0
0
Output should be 0

Re: 11821 - High-Precision Number

Posted: Tue Oct 07, 2014 4:28 pm
by mgavin2
idk what could be wrong with this supposedly easy problem.... something really stupid is catching me.

Code: Select all

thanks brianfry
So here's my code while I go do something else. Thank you :)

Re: 11821 - High-Precision Number

Posted: Tue Oct 07, 2014 9:01 pm
by brianfry713
Input:

Code: Select all

1
0.01
0.09
0
Output should be: 0.1

Re: 11821 - High-Precision Number

Posted: Tue Oct 07, 2014 11:19 pm
by mgavin2
omfg. Java BigDecimal method stripTrailingZeros only works some of the time (actually it seems to only work for zeros after the decimal point). Hence the stupidly looking use of compareTo.

Thank you brianfry

Re: 11821 - High-Precision Number

Posted: Wed Jan 13, 2016 3:34 am
by pointless0
Interesting point of difference between my AC submission and uDebug...

Test input:

Code: Select all

1
0.00000000000007
0
My AC result:

Code: Select all

0.00000000000007
uDebug result:

Code: Select all

7E-14
Should I consider my solution unacceptable?

Re: 11821 - High-Precision Number

Posted: Fri Mar 10, 2017 2:55 pm
by lighted
Problem descriprion says
All zeros after the decimal point located behind the last non-zero digit must be discarded
So your solution acceptable. uDebug gives same output as yours now.