forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy_efficient.c
56 lines (40 loc) · 1.26 KB
/
greedy_efficient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Description: This program gives the least amount of USD coins for a given amount efficiently.
NOTE: USD coin set is { 25, 10, 5, 1}
By randerson112358
*/
#include<stdio.h>
#define LEN 4 // The CONSTANT length of our array/coin set
//Greedy Algorithm for printing the least amount of change
void greedy(int amount);
int main(void){
int amount = 99;
greedy(amount);
system("pause");//Comment this out if you are not using Windows
}
void greedy(int amount){
int i=0;//index of the array
int number;//the number of least amount of coin for a specific coin value
int coin_set[LEN]= {25, 10, 5, 1};//The USD coin set
printf("The least amount of change needed for the amount %d is below: \n", amount);
while(i < LEN){
if(coin_set[i] <= amount){
number = amount/coin_set[i];
//Prints the least number of coins needed
if(coin_set[i] == 25 ){
printf("%d quarters (%d) \n", number, coin_set[i]);
}
if(coin_set[i] == 10 ){
printf("%d dimes (%d) \n", number, coin_set[i]);
}
if(coin_set[i] == 5 ){
printf("%d nickels (%d) \n", number, coin_set[i]);
}
if(coin_set[i] == 1 ){
printf("%d pennies (%d) \n", number, coin_set[i]);
}
amount = amount - number *coin_set[i];
}
i++;//increment i by 1
}
}