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
RookiE3
New poster
Posts: 16 Joined: Tue Feb 18, 2014 7:59 pm
Post
by RookiE3 » Thu Aug 21, 2014 7:26 pm
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
Post
by lighted » Tue Aug 26, 2014 6:36 pm
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
Post
by RookiE3 » Tue Aug 26, 2014 6:55 pm
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
Post
by brianfry713 » Thu Aug 28, 2014 12:01 am
Check input and AC output for thousands of problems on
uDebug !
mgavin2
New poster
Posts: 43 Joined: Sat Jul 28, 2012 6:29 pm
Post
by mgavin2 » Tue Oct 07, 2014 4:28 pm
idk what could be wrong with this supposedly easy problem.... something really stupid is catching me.
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
Post
by brianfry713 » Tue Oct 07, 2014 9:01 pm
Input:
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
Post
by mgavin2 » Tue Oct 07, 2014 11:19 pm
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
Post
by pointless0 » Wed Jan 13, 2016 3:34 am
Interesting point of difference between my AC submission and uDebug...
Test input:
My AC result:
uDebug result:
Should I consider my solution unacceptable?
lighted
Guru
Posts: 587 Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek
Post
by lighted » Fri Mar 10, 2017 2:55 pm
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