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

×

ATM , why this is giving wrong answer here?

include <stdio.h>

include <stdlib.h>

int main() { int x; float y; scanf("%f",&y); if(y>=0&&y<=2000) { scanf("%d",&x); if((x%5==0)&&x>=0&&x<=2000&&x<=y) { printf("%0.2f",y-(x+0.5)); } else printf("%0.2f",y); } return 0; }

asked 25 Jul '14, 22:49

usernametake's gravatar image

0★usernametake
11
accept rate: 0%


Here are the corrections:
1. You are first supposed to take x(amount to be withdrawn) as input and then y(balance). But you are first taking y as input and then x.
2. The extra 0.5 is also to be deducted from the balance, so this changes the condition in the inner if.
Correction: if((x%5==0)&&x>=0&&x<=2000&&x+0.5<=y)
[x<=y changed to x+0.5<=y]


Here is the corrected solution: http://www.codechef.com/viewsolution/4377646



There is also no need for checking x>=0&&x<=2000 and also y>=0&&y<=2000, these values simply tell you the range of the input for the problem. There are known as constraints.
The use of constraints:
The most basic use of constraints is that they let you decide the type of data type to use. Suppose if 0<=x<=1012, then you had to declare x as long long instead of int.
In more complex problems they help you decide the algorithm to use to solve the problem based on the time limit. You calculate the time complexity and check from the constraints that it will execute in the time limit specified.


So, there is no need for these checks, the input will definitely be in this range only.

Here is the solution without these checks: http://www.codechef.com/viewsolution/4377747

link

answered 25 Jul '14, 23:10

pratku123's gravatar image

4★pratku123
1.8k4932
accept rate: 14%

edited 25 Jul '14, 23:24

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: 25 Jul '14, 22:49

question was seen: 635 times

last updated: 25 Jul '14, 23:24