About the coin change in the backpack

Source: Internet
Author: User
There are 6 kinds of coins with different denominations. the denominations of each coin are 5 points, 1 angle, 2 angles, 5 angles, 1 yuan, and 2 yuan respectively. Now we need coins with these denominations to shop and find money. The number of Coins with different denominations that can be used for shopping is stored in the array Coins [], and there are enough Coins with different denominations in the store. You want to use the minimum number of coins for a single purchase. For example, if you need to pay 0.55 yuan for a single shopping, you have to use 2*20 + 10 + 5 coins for a total of 4 coins.

There are 6 kinds of coins with different denominations. the denominations of each coin are 5 points, 1 angle, 2 angles, 5 angles, 1 yuan, and 2 yuan respectively. Now we need coins with these denominations to shop and find money. The number of Coins with different denominations that can be used for shopping is stored in the array Coins [], and there are enough Coins with different denominations in the store. You want to use the minimum number of coins for a single purchase. For example, if you need to pay 0.55 yuan for a single purchase, you have to pay for four coins in total with 2*20 + 10 + 5 coins. If you pay 1 yuan and get 4 cents and 5 cents, you also need 4 coins. However, if you pay 1.05 yuan (1 yuan and 1 5 points), you only need 3 coins to retrieve the 5 corners. This solution uses the minimum number of coins.

For the number of coins and payment amount of each given nominal value, the transaction scheme with the minimum number of coins is calculated.

Input: There are several groups of Input data. Each row has 6 integers and 1 real number with 2 decimal places. Indicates the number of coins and the payment amount of each nominal value. The file ends at 6 zeros.

Output: outputs the calculated minimum number of coins. The result should be output by a branch, with one data row in each row. If the transaction cannot be completed, "impossible" is output ".

Sample Input:

2 4 2 2 1 0 0.952 4 2 0 1 0 0.550 0 0 0 0 0

Sample Output:

23

Solution: 01 backpack, full backpack.

Change [I] indicates the minimum number of coins required for the shop to pay the nominal value;

Dp [I] indicates that the number of existing coins paid by the customer is the minimum number of coins required by I;

W is the actual face value to be paid. if the customer pays k (k> = w), the merchant will find k-w, under this condition, the minimum number of coins required is dp [k] + change [k-w]. therefore, the minimum number of coins is k> = w. The minimum number of coins is dp [k] + change [k-w].

That is, ans = min (dp [k] + change [k-w]) (k> = w ).

For change [I], there are enough coins with different denominations in the store, so it can be implemented with a full backpack.

For dp [I], it can be computed using a hybrid backpack. here I split it into a 01 backpack (more violent, O (strong _ strong) O ~).

PS: To reduce the space overhead, it is calculated in 5 units.

# Include
 
  
# Include
  
   
Const int N = 20000; int change [N]; // change [I] is the minimum number of coins required for money with a nominal value of I. int dp [N]; // dp [I] indicates the minimum number of coins that the nominal value is I. int value [6] = {1, 2, 4, 10, 20, 40 }; // the nominal value of each coin, which is 100,200, or 40, that is, int number [6]. // corresponding to the number of each coin currently owned void init () // calculate change [I] {int I, j; for (I = 0; I
   
    
= Value [I]; j --) {if (dp [j-value [I]! =-1) {int temp = dp [j-value [I] + 1; if (dp [j] =-1 | temp
    
     
Temp) ans = temp ;}}// for (I = 0; I
     
      

This article is available at http://www.nowamagic.net/librarys/veda/detail/844.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.