PAT 1068. Find More Coins (30)

1068. Find More Coins (30)

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 104coins 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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=104, the total number of coins) and M(<=102, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V1 <= V2 <= ... <= Vk such that V1 + V2 + ... + Vk = M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].

Sample Input 1:
8 9
5 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 8
7 2 4 3
Sample Output 2:
No Solution

采用动态规划的思想,将coins从大到小进行排序,计算出每个从头开始的之序列(必须包含最后一个元素)所能够组合形成的值的大小,例如(9,8,7,5,4,3)必须有一个是3,然后所能形成的不大于9的值为3 7 8三个值,最后从小往上进行查找就可以得到相应的coins
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 int coins[10001];
 8 int money[10001][101];
 9 int all[10001][101];
10 vector<int> ToPay;
11 
12 void FindCoins(int amount, int index)
13 {
14     if (amount == 0)
15         return;
16     for (int i = index; i >= 0; i--)
17     {
18         if (money[i][amount] == 1)
19         {
20             ToPay.push_back(coins[i]);
21             FindCoins(amount - coins[i], i - 1);
22             return;
23         }
24     }
25 }
26 
27 bool cmp(int a, int b)
28 {
29     return a > b;
30 }
31 
32 int main()
33 {
34     int coinsNum, amountToPay;
35     cin >> coinsNum >> amountToPay;
36     for (int i = 0; i < coinsNum; i++)
37         cin >> coins[i];
38     sort(coins, coins + coinsNum, cmp);
39     //initialize
40     for (int i = 0; i < coinsNum; i++)
41         money[i][0] = all[i][0] = 1;
42     for (int i = 0; i < coinsNum; i++)
43     {
44         if (i == 0)
45         {
46             money[i][coins[i]] = all[i][coins[i]] = 1;
47             continue;
48         }
49         for (int j = 0; j <= amountToPay; j++)
50         {
51             if (all[i - 1][j] == 1)
52                 all[i][j] = 1;
53             if (all[i - 1][j] == 1 && j + coins[i] <= amountToPay)
54             {
55                 money[i][j + coins[i]] = 1;
56                 all[i][j + coins[i]] = 1;
57             }
58         }
59     }
60     if (all[coinsNum - 1][amountToPay] == 0)
61     {
62         cout << "No Solution";
63         return 0;
64     }
65     FindCoins(amountToPay, coinsNum-1);
66     
67     for (int i = 0; i < ToPay.size()-1; i++)
68         cout << ToPay[i] << " ";
69     cout << ToPay.back();
70 }

 

posted @ 2015-08-23 14:15  JackWang822  阅读(650)  评论(0编辑  收藏  举报