11821 - High-Precision Number

All about problems in Volume 118. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
RookiE3
New poster
Posts: 16
Joined: Tue Feb 18, 2014 7:59 pm

11821 - High-Precision Number

Post 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());
        }
    }
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11821 - High-Precision Number

Post 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.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
RookiE3
New poster
Posts: 16
Joined: Tue Feb 18, 2014 7:59 pm

Re: 11821 - High-Precision Number

Post by RookiE3 »

I have changed the code as you said, but still WA :(

Please tell me if logic is OK or not.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11821 - High-Precision Number

Post by brianfry713 »

Input:

Code: Select all

1
0.0
0
Output should be 0
Check input and AC output for thousands of problems on uDebug!
mgavin2
New poster
Posts: 43
Joined: Sat Jul 28, 2012 6:29 pm

Re: 11821 - High-Precision Number

Post 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 :)
Last edited by mgavin2 on Tue Oct 07, 2014 11:19 pm, edited 1 time in total.
all that matters is AC
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11821 - High-Precision Number

Post by brianfry713 »

Input:

Code: Select all

1
0.01
0.09
0
Output should be: 0.1
Check input and AC output for thousands of problems on uDebug!
mgavin2
New poster
Posts: 43
Joined: Sat Jul 28, 2012 6:29 pm

Re: 11821 - High-Precision Number

Post 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
all that matters is AC
pointless0
New poster
Posts: 9
Joined: Wed Jan 13, 2016 3:24 am

Re: 11821 - High-Precision Number

Post 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?
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11821 - High-Precision Number

Post 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.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Post Reply

Return to “Volume 118 (11800-11899)”