Back to Homepage
Back to ACM page
Back to Algorithms page
Problem: Given a set of n coins {1, 5, 10, 25, 50,...}, how many ways can a sum x be made?
i.e. x = 7. There are 6 ways to arrive at 7:
1 1 1 1 1 1 1
2 1 1 1 1 1
2 2 1 1 1
2 2 2 1
5 1
5 2
num [x] = { 1, 2, 5, 10, …. }
| 1 | 2 | 5 | 10 | … | |
| 0 | 1 | 1 | 1 | 1 | |
| 1 | 1 | 1 | 1 | 1 | |
| 2 | 1 | 2 | 2 | 2 | |
| 3 | 1 | 2 | 2 | 2 | |
| 4 | 1 | 3 | 3 | 3 | |
| 5 | 1 | 3 | 4 | 4 | |
| 6 | 1 | 4 | 5 | 5 | |
| 7 | 1 | 4 | 6 | 6 | |
| 8 | 1 | 5 | 7 | 7 | |
| 9 | 1 | 5 | 8 | 8 | |
| 10 | 1 | 6 | 10 | 11 | |
| 11 | 1 | 6 | 11 | 12 | |
| 12 | 1 | 7 | 13 | 15 | |
| 13 | 1 | 7 | 14 | 16 | |
| 14 | 1 | 8 | 16 | 19 | |
| 15 | 1 | 8 | 18 | 22 | |
| … |
table[x][0] = 1;
table[0][y] = 1;
table[x][y] = table[x-1][y] + table[x][y - num[x]];
Please report all broken links to jam0cam@yahoo.com
Created 7/20/06
Last Updated: 7/20/06