- 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; }

22nd May 2020, 9:38 AM
Rafael
5 odpowiedzi
+ 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
22nd May 2020, 7:26 PM
4lx
4lx - avatar
+ 1
Change 0.5 to 0.99 and this work...
22nd May 2020, 1:59 PM
4lx
4lx - avatar
0
Thank you! But what is the cause?
22nd May 2020, 7:03 PM
Rafael
0
Ah, now I get it! Thank you!
22nd May 2020, 7:28 PM
Rafael
0
why to add 0.99? can someone explain?
30th Nov 2020, 5:23 AM
Urmalveer Singh
Urmalveer Singh - avatar