1031: Minimum number of coins

Source: Internet
Author: User
1031: Minimum number of coinsTime limit: 1 Sec Memory limit: 128 MB
Submitted: 125 Settlement: 104
Submitted State [Discussion Version] Topic DescriptionThis is an old and classic question. There are generally several ways of forming a sum of money with a given number of coins. For example: given 6 kinds of coin face value of 2, 5, 10, 20, 50, 100, used to collect 15 yuan, can be 5 yuan, a 2 yuan, or 5 3 yuan, or a 5 yuan, a 5 yuan, and so on. Obviously, it takes at least 2 coins to make up 15 yuan.
Your task is, given a number of different coin face value, programming calculation, at least how many coins to make up a given amount of money. inputInput can have multiple test cases. The first line of each test case is the money to be pooled. Value M (1 <= m<= 2000, Integer), and then in a row, the first integer k (1 <= k <= 10) indicates the number of currencies, followed by K's distinct coin denomination ki (1 <= Ki <= 1 000). End when entering m=0. OutputEach test case outputs one row, which is the minimum number of coins needed to sum up the money value M. If the chip fails, the output "impossible". You can assume that the number of each type of pooled coin is infinitely large. Sample Input
6 2 5
1
1
2 0
Sample Output
2

Impossible

Improved code:

#include <iostream>
using namespace std;

int v[20];
int n, S; 
int minv[200],maxv[200];
int d[200]={0},vis[200] = {0};

int min (int x, int y)
{return
	x < y x:y;
}

Int. DP (int S)
{
	if (s = = 0) return 0;
	if (Vis[s]) return d[s];
	int &ans = D[s];
	Ans = (1<<30);
	for (int i = 0; i < n; i++)
		if (S >= v[i]) 
			ans = min (ans, DP (s-v[i)) + 1);
	return ans;
}

void Print_ans (int *d, int S) {for
	(int i = 0; i < n; i++)
		if (S >= v[i] && d[s] = = D[s-v[i]] + 1) {
			cout << v[i] << ';
			Print_ans (d, S-v[i]);
			break;
		}
}

int main ()
{
	minv[0] = maxv[0] = 0;
 	cout << "Enter the number of coins:"; 
	CIN >> N;
	cout << "Enter its face value in turn:" << Endl;
	for (int i = 0; i < n; i++)
		cin >> V[i];
	cout << "input to the total face value:";
	Cin >> S;
	cout << "The minimum number of coins required is:" << dp (S) << Endl << "They are respectively:";
	Print_ans (d, S);
	return 0; 
}


 

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.