您的位置:首页 > 其它

硬币找零问题

2012-07-22 13:27 239 查看
1. Question

Given a list of 'N' coins, their values being in an array A[], return the minimum number of coins required to sum to 'S' (you can use as many coins you want). If it's not possible to sum to 'S', return -1

Sample Test Cases:

Input #00:
Coin denominations: { 1,3,5 }
Required sum (S): 11

Output #00:
3

Explanation:
The minimum number of coins requires is: 3 - 5 + 5 + 1 = 11;

Input #01:
Coin denominations: { 5,5,5,5,5 }
Required sum (S): 11

Output #01:
-1

Explanation:
With the given coin denominations, it's not possible to get 11


参考:/article/10295663.html

2. solution

动态规划解,

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 0xFFFF;

int minCoins(vector< int > a , int S) {
//check the input
if(a.empty() || S < 0)
{
return -1;
}

// get the number of coins greedy
sort(a.begin(), a.end(), greater<int>());

int *cents = new int[S + 1];
for(int i = 1; i <= S; i++)
{
cents[i] = MAX;
}
cents[0] = 0;

for(int m = 1; m <= S; m++)
{
for(int i = 0; i < a.size(); i++)
{
if(m >= a[i])
{
int temp = cents[m-a[i]] + 1;
if(temp < cents[m])
{
cents[m] = temp;
}
}
}
cout << " for money m = " << m << ", need coin number " << cents[m] << endl;
}

if(cents[S] < MAX)
{
return cents[S];
}
else
{
return -1;
}
}

int main()
{
vector<int> a;
int n, S;
cin >> n >> S;
for(int i = 0; i < n; i++)
{
int x ;
cin >> x;
a.push_back(x);
}
cout << minCoins(a, S) << endl;
}


result,

>> cat input
5 63
1 2 5 21 25
[unix: /nomad2/c ]
>> cat input |./a.out
for money m = 1, need coin number 1
for money m = 2, need coin number 1
for money m = 3, need coin number 2
for money m = 4, need coin number 2
for money m = 5, need coin number 1
for money m = 6, need coin number 2
for money m = 7, need coin number 2
for money m = 8, need coin number 3
for money m = 9, need coin number 3
for money m = 10, need coin number 2
for money m = 11, need coin number 3
for money m = 12, need coin number 3
for money m = 13, need coin number 4
for money m = 14, need coin number 4
for money m = 15, need coin number 3
for money m = 16, need coin number 4
for money m = 17, need coin number 4
for money m = 18, need coin number 5
for money m = 19, need coin number 5
for money m = 20, need coin number 4
for money m = 21, need coin number 1
for money m = 22, need coin number 2
for money m = 23, need coin number 2
for money m = 24, need coin number 3
for money m = 25, need coin number 1
for money m = 26, need coin number 2
for money m = 27, need coin number 2
for money m = 28, need coin number 3
for money m = 29, need coin number 3
for money m = 30, need coin number 2
for money m = 31, need coin number 3
for money m = 32, need coin number 3
for money m = 33, need coin number 4
for money m = 34, need coin number 4
for money m = 35, need coin number 3
for money m = 36, need coin number 4
for money m = 37, need coin number 4
for money m = 38, need coin number 5
for money m = 39, need coin number 5
for money m = 40, need coin number 4
for money m = 41, need coin number 5
for money m = 42, need coin number 2
for money m = 43, need coin number 3
for money m = 44, need coin number 3
for money m = 45, need coin number 4
for money m = 46, need coin number 2
for money m = 47, need coin number 3
for money m = 48, need coin number 3
for money m = 49, need coin number 4
for money m = 50, need coin number 2
for money m = 51, need coin number 3
for money m = 52, need coin number 3
for money m = 53, need coin number 4
for money m = 54, need coin number 4
for money m = 55, need coin number 3
for money m = 56, need coin number 4
for money m = 57, need coin number 4
for money m = 58, need coin number 5
for money m = 59, need coin number 5
for money m = 60, need coin number 4
for money m = 61, need coin number 5
for money m = 62, need coin number 5
for money m = 63, need coin number 3
3


贪心算法+深度优先搜索解,求出所有的方案。

#include <iostream>
using namespace std;

int a[] = { 25, 10, 5, 1 };
int n[4];

static int num = 0;

void dfs(int s, int c) {
int p = sizeof(a) / sizeof(int);
if (c == p) {
if (s == 0) {
num = num + 1;
cout << "find a solution" << endl;
for (int i = 0; i < p; i++) {
cout << n[i] << " ";
}
cout << endl;
}
return;
}
for (int i = s / a[c]; i >= 0; i--) {
n[c] = i;
dfs(s - i * a[c], c+1);
}
}

int main() {
dfs(100, 0);
cout << "the number of solution is " << num << endl;
}


运行结果:
find a solution
4 0 0 0
find a solution
3 2 1 0
find a solution
3 2 0 5
find a solution
3 1 3 0
find a solution
3 1 2 5
find a solution
3 1 1 10
find a solution
3 1 0 15
find a solution
3 0 5 0
find a solution
3 0 4 5
find a solution
3 0 3 10
find a solution
3 0 2 15
find a solution
3 0 1 20
find a solution
3 0 0 25
find a solution
2 5 0 0
find a solution
2 4 2 0
find a solution
2 4 1 5
find a solution
2 4 0 10
find a solution
2 3 4 0
find a solution
2 3 3 5
find a solution
2 3 2 10
find a solution
2 3 1 15
find a solution
2 3 0 20
find a solution
2 2 6 0
find a solution
2 2 5 5
find a solution
2 2 4 10
find a solution
2 2 3 15
find a solution
2 2 2 20
find a solution
2 2 1 25
find a solution
2 2 0 30
find a solution
2 1 8 0
find a solution
2 1 7 5
find a solution
2 1 6 10
find a solution
2 1 5 15
find a solution
2 1 4 20
find a solution
2 1 3 25
find a solution
2 1 2 30
find a solution
2 1 1 35
find a solution
2 1 0 40
find a solution
2 0 10 0
find a solution
2 0 9 5
find a solution
2 0 8 10
find a solution
2 0 7 15
find a solution
2 0 6 20
find a solution
2 0 5 25
find a solution
2 0 4 30
find a solution
2 0 3 35
find a solution
2 0 2 40
find a solution
2 0 1 45
find a solution
2 0 0 50
find a solution
1 7 1 0
find a solution
1 7 0 5
find a solution
1 6 3 0
find a solution
1 6 2 5
find a solution
1 6 1 10
find a solution
1 6 0 15
find a solution
1 5 5 0
find a solution
1 5 4 5
find a solution
1 5 3 10
find a solution
1 5 2 15
find a solution
1 5 1 20
find a solution
1 5 0 25
find a solution
1 4 7 0
find a solution
1 4 6 5
find a solution
1 4 5 10
find a solution
1 4 4 15
find a solution
1 4 3 20
find a solution
1 4 2 25
find a solution
1 4 1 30
find a solution
1 4 0 35
find a solution
1 3 9 0
find a solution
1 3 8 5
find a solution
1 3 7 10
find a solution
1 3 6 15
find a solution
1 3 5 20
find a solution
1 3 4 25
find a solution
1 3 3 30
find a solution
1 3 2 35
find a solution
1 3 1 40
find a solution
1 3 0 45
find a solution
1 2 11 0
find a solution
1 2 10 5
find a solution
1 2 9 10
find a solution
1 2 8 15
find a solution
1 2 7 20
find a solution
1 2 6 25
find a solution
1 2 5 30
find a solution
1 2 4 35
find a solution
1 2 3 40
find a solution
1 2 2 45
find a solution
1 2 1 50
find a solution
1 2 0 55
find a solution
1 1 13 0
find a solution
1 1 12 5
find a solution
1 1 11 10
find a solution
1 1 10 15
find a solution
1 1 9 20
find a solution
1 1 8 25
find a solution
1 1 7 30
find a solution
1 1 6 35
find a solution
1 1 5 40
find a solution
1 1 4 45
find a solution
1 1 3 50
find a solution
1 1 2 55
find a solution
1 1 1 60
find a solution
1 1 0 65
find a solution
1 0 15 0
find a solution
1 0 14 5
find a solution
1 0 13 10
find a solution
1 0 12 15
find a solution
1 0 11 20
find a solution
1 0 10 25
find a solution
1 0 9 30
find a solution
1 0 8 35
find a solution
1 0 7 40
find a solution
1 0 6 45
find a solution
1 0 5 50
find a solution
1 0 4 55
find a solution
1 0 3 60
find a solution
1 0 2 65
find a solution
1 0 1 70
find a solution
1 0 0 75
find a solution
0 10 0 0
find a solution
0 9 2 0
find a solution
0 9 1 5
find a solution
0 9 0 10
find a solution
0 8 4 0
find a solution
0 8 3 5
find a solution
0 8 2 10
find a solution
0 8 1 15
find a solution
0 8 0 20
find a solution
0 7 6 0
find a solution
0 7 5 5
find a solution
0 7 4 10
find a solution
0 7 3 15
find a solution
0 7 2 20
find a solution
0 7 1 25
find a solution
0 7 0 30
find a solution
0 6 8 0
find a solution
0 6 7 5
find a solution
0 6 6 10
find a solution
0 6 5 15
find a solution
0 6 4 20
find a solution
0 6 3 25
find a solution
0 6 2 30
find a solution
0 6 1 35
find a solution
0 6 0 40
find a solution
0 5 10 0
find a solution
0 5 9 5
find a solution
0 5 8 10
find a solution
0 5 7 15
find a solution
0 5 6 20
find a solution
0 5 5 25
find a solution
0 5 4 30
find a solution
0 5 3 35
find a solution
0 5 2 40
find a solution
0 5 1 45
find a solution
0 5 0 50
find a solution
0 4 12 0
find a solution
0 4 11 5
find a solution
0 4 10 10
find a solution
0 4 9 15
find a solution
0 4 8 20
find a solution
0 4 7 25
find a solution
0 4 6 30
find a solution
0 4 5 35
find a solution
0 4 4 40
find a solution
0 4 3 45
find a solution
0 4 2 50
find a solution
0 4 1 55
find a solution
0 4 0 60
find a solution
0 3 14 0
find a solution
0 3 13 5
find a solution
0 3 12 10
find a solution
0 3 11 15
find a solution
0 3 10 20
find a solution
0 3 9 25
find a solution
0 3 8 30
find a solution
0 3 7 35
find a solution
0 3 6 40
find a solution
0 3 5 45
find a solution
0 3 4 50
find a solution
0 3 3 55
find a solution
0 3 2 60
find a solution
0 3 1 65
find a solution
0 3 0 70
find a solution
0 2 16 0
find a solution
0 2 15 5
find a solution
0 2 14 10
find a solution
0 2 13 15
find a solution
0 2 12 20
find a solution
0 2 11 25
find a solution
0 2 10 30
find a solution
0 2 9 35
find a solution
0 2 8 40
find a solution
0 2 7 45
find a solution
0 2 6 50
find a solution
0 2 5 55
find a solution
0 2 4 60
find a solution
0 2 3 65
find a solution
0 2 2 70
find a solution
0 2 1 75
find a solution
0 2 0 80
find a solution
0 1 18 0
find a solution
0 1 17 5
find a solution
0 1 16 10
find a solution
0 1 15 15
find a solution
0 1 14 20
find a solution
0 1 13 25
find a solution
0 1 12 30
find a solution
0 1 11 35
find a solution
0 1 10 40
find a solution
0 1 9 45
find a solution
0 1 8 50
find a solution
0 1 7 55
find a solution
0 1 6 60
find a solution
0 1 5 65
find a solution
0 1 4 70
find a solution
0 1 3 75
find a solution
0 1 2 80
find a solution
0 1 1 85
find a solution
0 1 0 90
find a solution
0 0 20 0
find a solution
0 0 19 5
find a solution
0 0 18 10
find a solution
0 0 17 15
find a solution
0 0 16 20
find a solution
0 0 15 25
find a solution
0 0 14 30
find a solution
0 0 13 35
find a solution
0 0 12 40
find a solution
0 0 11 45
find a solution
0 0 10 50
find a solution
0 0 9 55
find a solution
0 0 8 60
find a solution
0 0 7 65
find a solution
0 0 6 70
find a solution
0 0 5 75
find a solution
0 0 4 80
find a solution
0 0 3 85
find a solution
0 0 2 90
find a solution
0 0 1 95
find a solution
0 0 0 100
the number of solution is 242
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: