Find Optimum number of coins for a given money input

This is a post in which you can use the bellow program to find the optimum number of coins which can substitute the given money. You can infinite 1re, 2rs and 5 rs coin and you want to arrange the input in such a way that you will carry less number of coin with u. This program is not a knapsack implementaion. This program is also not done using the Greedy Approach. This code works fine for all input case. The below program is done using python 2.7

Screenshots/ Demo


Above is the screenshot showing the various test cases 

The Code:
def coins(x):
    fi=0
    tw=0
    on=0
    while(x>0):
        if(x>=5):
            x=x-5;
            fi=fi+1
        elif (x>=2):
            x=x-2;
            tw=tw+1
        else:
            x=x-1;
            on=on+1;
    print "five: "+str(fi)+"\ntwo:"+str(tw)+"\none:"+str(on)

x=input()
coins(x)


Found Problems, Do place your comment ! ;)

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank String Reduction Solution

Bit Stuffing Code Implementation in Java