Created
April 17, 2014 00:45
-
-
Save mitrnsplt/10945388 to your computer and use it in GitHub Desktop.
A solution for cs50's "greedy" problem: making change with as few coins as possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* greedy.c | |
* | |
* Peter Downs | |
* | |
* A program for efficiently making change. | |
* | |
*/ | |
#include <cs50.h> | |
#include <math.h> | |
#include <stdio.h> | |
int main(void) | |
{ | |
// Declare and initialize a variable and ask for user input. | |
float a = 0; | |
do | |
{ | |
printf("How much change do we owe you? "); | |
a = GetFloat(); | |
} | |
while (a < 0); | |
// Change to cents and round to the nearest integer. | |
int c; | |
a = a*100; | |
a = roundf(a); | |
c = (int)a; | |
// Declare and initialize variables. | |
int counter = 0; | |
int quarters = 0; | |
int dimes = 0; | |
int nickels = 0; | |
int pennies = 0; | |
// Determine number of quarters. | |
quarters = c/25; | |
// Determine number of dimes. | |
int d = c%25; | |
dimes = d/10; | |
// Determine number of nickels and pennies. | |
int n = d%10; | |
nickels = n/5; | |
pennies = n%5; | |
// Sum the number of coins and print answer. | |
counter = quarters + dimes + nickels + pennies; | |
printf("%d\n", counter); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment