You are not logged in. Please login at www.codechef.com to post your questions!

×

ATM help getting wrong answer

import java.util.*;

/ * @author Nitish / class Atm {

public static void main(String[] args) 
{
    // TODO code application logic here
    Scanner in=new Scanner(System.in);
    int y=in.nextInt();
    double balance=in.nextDouble();
    //System.out.println(y+" "+balance);
    if(y%5==0 && y<balance)
    {
        balance = balance - y-0.50;
        System.out.println(balance);
    }
    else if(y>=balance)
    {
        System.out.println(balance);
    }
    else
    {
        System.out.println(balance);
    }
}

}

what is the problem in ans it's showing me wrong answer??

asked 20 May '12, 14:14

nitishmittal's gravatar image

0★nitishmittal
1111
accept rate: 0%

CAN ANYONE TELL ME PLEASE WHY I AM GETTING WRONG ANSWER?

#include<iostream>

include<iomanip>

using namespace std; using std::setprecision;

int main() { int a; float b; cin>>a; if(a>0) { cin>>b; cout<<"\t"; if(b>0||b>0) { if(a%5==0) { if(b>a) { cout<<setprecision(2)<<(b-a-0.5); }} else cout<<b; } } return 0; }

(02 Mar '14, 13:30) aggrwll0★

There are a couple of bugs in your code

(1)Did you read this line carefully in the problem statement :Output the number with two digits of precision.?

Your code block System.out.println(balance) is not printing the output in two digit precision. So replace the above line with

System.out.printf("%.2f\n",balance);//Prints output in two digits precesions

(2)The if conditions used is wrong

        if(y%5==0 && y<balance)
        {
            balance = balance - y-0.50;
            System.out.println(balance);
        }

I have removed the bug in your java code,now it will get accepted.

    import java.util.*;

    class  Atm
    {

    public static void main(String[] args)
    {
            // TODO code application logic here
            Scanner in=new Scanner(System.in);
            int y=in.nextInt();
            double balance=in.nextDouble();
            //System.out.println(y+" "+balance);
            if(y%5==0 && (y+.50)<=balance)
            {
            balance = balance - y-0.50;
            }
            System.out.printf("%.2f\n",balance);
            }
    }

PS.Don't use scanner for taking input in java ,please use BufferedReader as it is several times faster than scanner. In this problem ,it didn't cause problem but will trouble you in those problem that has large input file

link

answered 21 May '12, 12:52

ritesh_gupta's gravatar image

5★ritesh_gupta ♦
3.7k42549
accept rate: 27%

Output formatting is not an issue here, see my submission - http://www.codechef.com/viewsolution/1050984. I'm not a fan of "look, this is working solution" approach, I think it's better to show counter example and let them find own bugs...

(22 May '12, 02:49) betlista ♦♦3★

Try input

30 30.20

;-)

link

answered 20 May '12, 17:03

betlista's gravatar image

3★betlista ♦♦
16.9k49115225
accept rate: 11%

import static java.lang.Math.abs; import java.util.Scanner; class Sample1 { public static void main(String[] args){ Scanner scan = new Scanner(System.in); double per = scan.nextDouble(); float s =0.50f; double a=120.00,b; if((per%5)==10){ b = abs(per-120.00); System.out.println(b-(s)); } else{ System.out.println(a); }

}

}

link

answered 25 Jan '15, 18:06

sri2015's gravatar image

0★sri2015
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×190

question asked: 20 May '12, 14:14

question was seen: 1,330 times

last updated: 25 Jan '15, 18:06