0

The check50 displays

jharvard@appliance (~/Dropbox/pset1): check50 2014/x/pset1/greedy greedy.c    
:) greedy.c exists  
:) greedy.c compiles   
:) input of 0.41 yields output of 4  
:) input of 0.01 yields output of 1  
:) input of 0.15 yields output of 2   
:) input of 1.6 yields output of 7   
:) input of 23 yields output of 92   
:( input of 4.2 yields output of 18   
   \ expected output, but not "22\n"  
:) rejects a negative input like -.1  
:) rejects a non-numeric input of "foo"   
:) rejects a non-numeric input of ""   

I understand that this

:( input of 4.2 yields output of 18
    \ expected output, but not "22\n" 

is causing the error.

But if 4.2 is considered as 4.20 then 22 is the correct answer and 18 is correct if it is considered as 4.02.

Then again in the previous check 1.6 is considered as 1.60.

So, 22 should have been considered correct???

1 Answer 1

1

4.2 (which is 420 cents) is equal to 4.20 (which is also 420 cents), but both are NOT equal to 4.02 (which is 402 cents).

The maximum number of quarters (25 cents) in 420 cents is 16 and the remainder is 20 cents (420 - (16 x 25) = 420 - 400 = 20).

The maximum number of dimes in the remaining 20 cents is 2 (that's obviously 2 x 10).

This gives us a total of 16 quarters + 2 dimes = 18 coins.

Similarly, 1.6 is equal to 1.60, but both are NOT equal to 1.06.

2
  • But how to decide this problem then? What шs wrong with the code itself?
    – user13645
    Commented Sep 7, 2016 at 22:04
  • @veravera, recall floats are not precise. 4.2 doesn't get stored exactly as 4.2, which leads to a problem when converting it to cents, unless you're doing something hinted at in the specs ;)
    – kzidane
    Commented Sep 7, 2016 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .