Nyoj 995 coin change

Source: Internet
Author: User
Coin changeTime Limit: 1000 MS | memory limit: 65535 kb difficulty: 3
Description
In real life, we often encounter coin-seeking issues. For example, when sending a salary, financial personnel need to calculate the minimum number of coin-seeking, so that they can get the minimum number of coins from the bank and make sure they can pay for the coins. We should note that the RMB coin system is 0.5, 50, 20, 10, 5, 2, 1, 0.2, 0.1, 0.05, 0.02, 0.01, Yuan, using these coins, we can use greedy algorithms to find the minimum number of coins for any wage. However, unfortunately, we may not have such a good coin system. Therefore, we cannot use greedy algorithms to find the minimum number of coins. Even some coins cannot be used to change the total number of coins. For example, if the coin system is 40, 30, 25 yuan, then 37 Yuan won't be able to use these coins to change to zero; 95 yuan's minimum change to zero Coin number is 3. For example, if the coin system is 10, 7, 5, or 1 yuan, then 12 yuan uses the greedy method to get 3 coins, and the minimum number of coins is 2. Your task is: For any coin system and a coin number, please program to find the minimum number of coins; if you cannot use these coins to find the number, please give a way to find the zero, minimize the remaining money.
 
Input
Input data:
Rows 1: N and T. 1 ≤ n ≤ 50 indicates the number of coins in the coin system. 1 ≤ T ≤ 1st indicates the total number of coins to be changed.
2nd behavior n positive integers not greater than 65535, which are the denominations of each coin in the coin system.
End when N and T are both 0.
Output
Output Data:
If t can be changed to zero by a coin in the coin system, please output the minimum number of coins.
If t cannot be changed to zero by a coin in the coin system, output the minimum number of coins in the change plan with the lowest remaining amount of money.
Sample Input
4 1210 7 5 1
Sample output
2
Source
Love life
Uploaded
TCM _ Zhang Peng


Solution: A full backpack ....

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 int num[60],dp[100010]; 7 int main() { 8     int n, i, m, j; 9     while(scanf("%d%d",&n,&m),n||m) {10         for(i = 0; i < n; i++)11             scanf("%d", &num[i]);12         for(i = 1; i <= m; i++)13             dp[i] = 100000;14         dp[0] = 0;15         for(i = 0; i < n; i++) {16             for(j = 1; j <= m; j++) {17                 if(j >= num[i]) {18                     dp[j] = min(dp[j], dp[j-num[i]]+ 1);19                 }20             }21         }22         for(i = m; i >= 0; i--) {23             if(dp[i] != 100000) break;24         }25         printf("%d\n", dp[i]);26     }27     return 0;28 }
View code

 



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.