change.cpp

(plain text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <climits>
using namespace std;

int main (int argc, char *argv[]) {
  const int denom[] = { INT_MAX, 1000, 500, 100, 50, 20, 10, 5, 1 };
  enum { nDenom = sizeof(denom) / sizeof(*denom) };

  int num = 1234;
  for (int i = 1; i < nDenom; ++i)
    if (int count = (num % denom[i - 1]) / denom[i])
      cout << count << " x $" << denom[i] << endl;
}