Computer Science, asked by rakeshchennupati143, 1 year ago

You have x no. of 5 rupee coins and y no. of 1 rupee coins. You want to purchase an item for amount z. The shopkeeper wants you to provide exact change. You want to pay using minimum number of coins. How many 5 rupee coins and 1 rupee coins will you use?

Write a python program which takes input of total_money_to_be_paid and no_of_fives_available and no_of_ones_available respectively, and print the output of no_of_fives_needed and no_of_ones_needed, If exact change is not possible then display -1, write the whole logic in a function and call the function using user given values as parameters.

Note: you have to pay using least number in the coins you have provided.

please find the image below to understand sample input and expected output :)

Attachments:

Answers

Answered by fiercespartan
8

total_cost,f_available,o_available = int(input('Total:')),int(input('5 coins available')),int(input('1 coins available'))

f_coins_needed = total_cost//5

left = total_cost - (5*f_coins_needed)

o_coins_needed = left // 1

if o_coins_needed <= o_available and f_coins_needed <= f_available:

   print('rs.1 coins needed :%d' %(o_coins_needed), 'rs.5 coins needed: %d' %(f_coins_needed))

else:

   print('-1')


rakeshchennupati143: what if you give input as 105-total and 20-five's and 5-one's
rakeshchennupati143: the output should be fives needed - 20 and one's needed - 5
rakeshchennupati143: but the output is getting as -1
fiercespartan: Let me check
rakeshchennupati143: yea...!
fiercespartan: ohh, when the output is like that... it is taking the number of 5_coins as 21
rakeshchennupati143: yea i think the problem lies at // coz the output of that will be floored so it is adding extra 1
Answered by sswaraj04
9

Answer:

Explanation:

one=input("no. of coins of one rupees")

five=input("no. of notes of five rupees")

val=input("amount to be paid")

x=val/5

y=val%5

while True:

if y<=one:

 if x<=five:

    print "no of one rupees coins is %d" %y

    print "no. of five rupees notes is %d" %x

    break

 else:

    x=x-1

    y=y+5

else:

    print "-1"

    break

Hope it's correct :-)


rakeshchennupati143: yes of cource i can
sswaraj04: ??
rakeshchennupati143: i will give you al the test case jus hold i'm running your program again
rakeshchennupati143: all*
rakeshchennupati143: bro do u use any social media so that i can send you the pic of all the test cases that are wrong
sswaraj04: just send me 2 cases here-- that will help
sswaraj04: Kerala
sswaraj04: Goa
rakeshchennupati143: no i mean fb or others
rakeshchennupati143: do u have so that we can chat there its bit uncomfortable here tho
Similar questions