# PAT 解题报告 1068. Find More Coins (30)

### 1068. Find More Coins题目描述：

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

### 1068. Find More Coins算法分析：

F(N, M) = max{ F(N – 1, M), F(N – 1, M – V(N)) + V(N) }, V(N)表示第N个硬币的面值

```#include <cstdio>
#include <cmath>
#include <algorithm>

const int ncoins = 10005;
const int nmonay = 105;

bool cmp(const int &a, const int &b) {
return a > b;
}

int main() {
int dp[ncoins][nmonay];
int  coins[ncoins];
bool isIncluded[nmonay][ncoins];

int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d", &coins[i]);
}
std::sort(coins + 1, coins + n + 1, cmp);

for(int i = 1; i <= m; ++i)
for(int j = 1; j <= n; ++j)
isIncluded[i][j] = false;

for(int i = 0; i <= m; ++i) dp[0][i] = 0;
for(int i = 0; i <= n; ++i) dp[i][0] = 0;

for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) {
if( j < coins[i] || dp[i - 1][j] >  coins[i] + dp[i - 1][j - coins[i]] ) {
dp[i][j] = dp[i - 1][j];
}
else {
dp[i][j] = dp[i - 1][j - coins[i]] + coins[i];
isIncluded[j][i] = true;
}
}

if(dp[n][m] != m) {
printf("No Solution\n");
return 0;
}

bool flag = true;
for(int i = n; i >= 1 && m > 0; --i) {
if(isIncluded[m][i]) {
if(flag) {
printf("%d", coins[i]);
flag = false;
}
else
printf(" %d", coins[i]);
m -= coins[i];
}
}
printf("\n");
return 0;
}
```

### 1068. Find More Coins注意点：

Written on October 14, 2013