Professional Documents
Culture Documents
Cal Chocolates
Cal Chocolates
Cal Chocolates
Let's say you have m Rupees and one chocolate costs rupees c.
The shopkeeper will give you a bar of chocolate for free if you give him 3
wrappers.
Can you determine how many chocolates can you get with the m Rupees you have?
x%3==0
x%3==1
x%3==2
first one is simple
in second case we can use- result of modulus i.e=1 as base, x//3==this value
second solution:-
given input(amount,cost_of_each_chocolate)
chocolates=amount//cost_of_each_chocolate
wrapper=0
if chocolates%3==0:
wrapper=chocolates
if wrapper%3==0:
wrapper=wrapper//3
chocolates=chocolates+wrapper
elif wrapper%3==1:
wrapper=(wrapper+1)//3
chocolates=chocolates+wrapper
elif wrapper%3==2:
wrapper=(wrapper+2)//3
chocolates=chocolates+wrapper
elif chocolates%3==1:
wrapper=chocolates
if wrapper%3==0:
wrapper=(wrapper+1)//3
chocolates=chocolates+wrapper
elif wrapper%3==1:
wrapper=(wrapper+2)//3
chocolates=chocolates+wrapper
elif wrapper%3==2:
wrapper=(wrapper+3)//3
chocolates=chocolates+wrapper
elif chocolates%3==2:
wrapper=chocolates
if wrapper%3==0:
wrapper=(wrapper+2)//3
chocolates=chocolates+wrapper
elif wrapper%3==1:
wrapper=(wrapper+3)//3
chocolates=chocolates+wrapper
elif wrapper%3==2:
wrapper=(wrapper+4)//3
chocolates=chocolates+wrapper