halloween candy solution! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

halloween candy solution!

anyone have a better solution? Mine works but it feels janky: houses = int(input()) #your code goes here dollarchance = 2 / houses percent = (dollarchance * 100) + .5 print(round(percent))

20th Mar 2020, 6:29 PM
Brenner Pieszak
Brenner Pieszak - avatar
11 Answers
+ 5
Here you go. In Java. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int houses = input.nextInt(); //your code goes here double percent =200; //your code goes here if(houses>=3){ percent /=houses; System.out.println((int)Math.ceil(percent)); } } }
21st Apr 2021, 9:00 AM
Joe Hutchens
Joe Hutchens - avatar
+ 4
Brenner Pieszak has added 0.5 as he is using Math.round The 0.5 will tip any random fraction to over 0.5, but under 1.5 which will always be rounded to 1 When we compare with Ore, the formula used here is Math.ceil which in itself does the work of adding 0.5. Hope what I said is comprehensible 😊
6th Jun 2021, 6:13 PM
Rafique
Rafique - avatar
+ 3
0.5 is used because if the round function is used on its own, if the number after the decimal is less than 5 (such as 30.4) then it will return 30, whereas we want it to return 31, using "+0.5" turns "30.4 into "30.9" which can then be used by the round function to make 31. Hope that helps :)
28th May 2022, 4:50 PM
LMR
+ 2
print(math.ceil((2 / houses ) * 100))
20th Mar 2020, 7:29 PM
Ore
Ore - avatar
+ 2
houses = int(input()) #your code goes here print (round(2 / houses * 100 + 0.5) )
3rd Aug 2023, 10:52 AM
Mohammed Ibrahim
Mohammed Ibrahim - avatar
+ 1
sorry, can someone explain to me why add `+0.5` to percent?
18th Nov 2020, 4:36 PM
Andrés Camilo Atencia Ortega
Andrés Camilo Atencia Ortega - avatar
0
this is for the code coach halloween candy btw!
20th Mar 2020, 6:31 PM
Brenner Pieszak
Brenner Pieszak - avatar
0
Awesome, thanks!
20th Mar 2020, 7:47 PM
Brenner Pieszak
Brenner Pieszak - avatar
0
import math houses = int(input()) print(math.ceil((100/houses) * 2))
30th Oct 2022, 9:54 PM
Hain
Hain - avatar
0
Cpp?? Anyone???
12th May 2023, 10:09 PM
Shapa
Shapa - avatar
0
This is for you C# bros out there.😎 using System; double houses; double percent; houses = Convert.ToInt32(Console.ReadLine()); percent = Convert.ToInt32((2 / houses) * 100 + 0.5); Console.WriteLine(percent);
22nd Sep 2023, 9:39 PM
Hein Htet
Hein Htet - avatar