lanobezkari's blog

By lanobezkari, 20 months ago, In English

Hi everyone,

I'm trying to solve this problem.

But my program gives wrong answer for this test case:

1
3 85
33 7 85

cause of the error is because normally

ceil(( 100.00 / 85.00 ) * 85.00 ) = 100

but my program returns answer as 101.

I guess its because CPP can't find exact result of ( 100 / 85 ) so it returns ( 100.00 / 85.00 ) * 85.00 ) something like 100.000001 and ceil function rounds the number up.

Is there a way to fix this?

Thanks in advance.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
20 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You should probably read this.

TLDR: You should never use floor() or ceil() in c++ if you're dealing with integers. And in fact you should never use any floating-point numbers if you're only dealing with integers.

In order to calculate ceil(a / b) for integers, do (a + b - 1) / b, but be careful with negative numbers as the behaviour may be unexpected.

It seems that in your case, you might need to rewrite your calculations a little bit before you can get this to work.