- 1
Halloween Candy
Why does this only work on the first two runs? #include <stdio.h> int main() { int houses; scanf("%d", &houses); //your code goes here float p = 200.0 / houses; float r = p + 0.5; int s = p; int t = r; if(s == t) { printf("%d \n", t); } else { printf("%d \n", t); } return 0; }
5 Answers
+ 2
I will give you an example:
The houses in input 4 are 66, p = 200/66 = 3.030303, r = p + 0.5 = 3.53, if you use int () in 3.53 it rounds down, so the result is 3. If you add 0.99 to p, r is 4.02, if you use int in 4.02 the result is 4
PS: In "Halloween Candy" you have to round up
PS: I want to explain better but my english is bad, sorry
+ 1
Change 0.5 to 0.99 and this work...
0
Thank you! But what is the cause?
0
Ah, now I get it! Thank you!
0
why to add 0.99? can someone explain?