Skip to content

Instantly share code, notes, and snippets.

@snadahalli
Last active September 20, 2017 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snadahalli/ba3675bad57dae3341713c23161ca318 to your computer and use it in GitHub Desktop.
Save snadahalli/ba3675bad57dae3341713c23161ca318 to your computer and use it in GitHub Desktop.
A simple solution for Change-making problem based on greedy method.
#include <stdio.h>
int main () {
int num_denominations, coin_list[100], use_these[100], i, owed;
printf("Enter number of denominations : ");
scanf("%d", &num_denominations);
printf("\nEnter the denominations in descending order: ");
for(i=0; i< num_denominations; i++) {
scanf("%d", &coin_list[i]);
// use_these[i] = 0;
}
printf("\nEnter the amount owed : ");
scanf("%d", &owed);
for(i=0; i < num_denominations; i++) {
use_these[i] = owed / coin_list[i];
owed %= coin_list[i];
}
printf("\nSolution: \n");
for(i=0; i < num_denominations; i++) {
printf("%dx%d ", coin_list[i], use_these[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment