Giter Site home page Giter Site logo

cs50-cash's Introduction

coins

cash.c

When dispensing change for customers, you would want to minimize the number of coins being dispensed. Using greedy algorithm, I solved the minimum number of coins that is due upon dispensing.

What is a greedy algorithm?

According to the National Institute of Standards and Technology (NIST), a greedy algorithm is one β€œthat always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.”

How is greedy algorithm usefull for coin dispensing?

Say we have a customer owed $1.10 in change. We could give 110 pennies, or 11 dimes, or even 22 nickels. With greedy algorithm or largest-to-smallest approach, we can get the fewest number of coins possible. We start with quarters, then move to dimes, nickels, and pennies. In our case, we can use 4 quarters to add up to $1 and 1 dime to equal #1.10. Hence, we would have 5 coins as the overall optimal solution to this problem of finding the minimum number of coins dispensed as the change.

Program should behave as below examples:

$ ./cash
Change owed: 0.41
4
$ ./cash
Change owed: 1.10
5

How to run the programs?

  1. Compile by running make cash or clang -o cash cash.c in terminal
  2. Run the program by running ./cash

Word of caution when compiling

You must install CS50 library for C in order to compile. Installation guide is here.

cs50-cash's People

Contributors

joseph-gansukh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.