Play with Digits(背包变形)

Description

Prof. Meng likes playing with digits. He finds a store selling digits made from some expensive metal. Different kinds of digits may be sold at different prices, and we may assume that the amount of each kind of digits is unlimited. Prof. Meng wants to find out the largest number he can create using no more than his total budget of M units of money. Could you help him?

For example, the store sells three kinds of digits, 0, 1, and 2, at prices of 5, 7, and 8 units of money respectively. If Prof. Meng has 14 units of money, the largest number he can create is 20, which costs him 13 (8 + 5) units of money.

Input

The input begins with a line containing an integer T (T<=80), which indicates the number of test cases.

Each case has two lines. The first line contains 10 integers in one line. The ith integer represents price of digit i – 1. Each price is between 1 and 50. -1 denotes that the corresponding digit is not on sale. The second line contains an integer M (1<=M<=50), indicating Prof. Meng’s total budget. It is guaranteed that Prof. Meng can buy at least one digit.

Output

For each case, output the largest possible number Prof. Meng can create in one line. The answer should not contain any extra leading zeroes.

Sample Input
 Copy sample input to clipboard
3
5 7 8 -1 -1 -1 -1 -1 -1 -1
14
1 2 3 4 5 6 7 8 9 10
1
1 1 1 1 -1 1 -1 1 1 1
3
Sample Output
20
0
999


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
int bud;
const int maxn=1000000000;
string go(string x,int j)
{
	int length=x.size();
	if (length==1 && x[0]=='a')
	{
		x[0]=j+'0';
		return x;
	}
	x.resize(length+1);
	x[length]=j+'0';
	return x;
}

bool judge(string a,string b,int pos)
{
	if (b[b.size()-1]=='0' && pos==bud)
		return true;
	if (b.size()==1 && b[0]=='a')
		return true;
	if (a.size()>b.size())
		return true;
	if (a.size()<b.size())
		return false;

	else
	{
		int i;
		for (i=a.size()-1;i>=0;i--)
			if (a[i]==b[i])
				continue;
			else if (a[i]>b[i])
				return true;
			else
				return false;
		return false;
	}
}



int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{

		string dp[105];
		int i;
		int A[10];
		for (i=0;i<=100;i++) dp[i]="a";

		for (i=0;i<=9;i++) scanf("%d",&A[i]);
		scanf("%d",&bud);

		int j;
		for (j=0;j<=9;j++)
		{
			for (i=1;i<=bud;i++)
			{
				if (A[j]!=-1 && i>=A[j] )
				{
					string a=go(dp[i-A[j]],j);
					//cout << "a " << a << endl;
					if (judge(a,dp[i],i))
						dp[i]=a;
				}//printf("%d ",dp[i]);
			//cout << dp[i] << ' ';
			}
			//printf("\n");
		}
		if (dp[bud][dp[bud].size()-1]=='0')
			printf("0\n");
		else
		{
		for (i=dp[bud].size()-1;i>=0;i--)
			printf("%c",dp[bud][i]);
		printf("\n");
		}
	}
	return 0;
}

做了我好久。。智商压制


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值