Skip to content

Instantly share code, notes, and snippets.

@jordansinger
Last active May 4, 2016 19:49
Show Gist options
  • Save jordansinger/b4923300fc8ff4d82a8d86151f933a95 to your computer and use it in GitHub Desktop.
Save jordansinger/b4923300fc8ff4d82a8d86151f933a95 to your computer and use it in GitHub Desktop.
change.c
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
int denominations[] = { 10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1 };
int NUM_DENOMINATIONS = sizeof(denominations) / sizeof(denominations[0]);
int count[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int main(int argc, char const *args[]) {
int due = atof(args[1]) * 100;
int paid = 0;
printf("Due: %.2f\n---\n", due / 100.0);
if(due != 0) {
for (int i = 0; i < NUM_DENOMINATIONS; i++) {
while (due >= 0 && denominations[i] <= due) {
due -= denominations[i];
count[i]++;
}
}
}
for (int i = 0; i < NUM_DENOMINATIONS; ++i) {
if (count[i] > 0) {
paid += count[i] * denominations[i];
printf("%i x %.2f = %.2f\n", count[i], denominations[i] / 100.0, paid / 100.0);
}
}
printf("---\nRemaining: %i\n", due);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment