爸爸去哪儿



#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> fruits;

bool cmp(int a, int b)
{
	return a > b;
}

int change(int leftValue, int useCnt)
{
	if (leftValue < 0)
		return -1;
	if (leftValue == 0)
		return useCnt;
	for (vector<int>::iterator it = fruits.begin(); it != fruits.end(); ++it)
	{
		int value = *it;
		int min = change(leftValue - value, useCnt + 1);
		if (min > 0) return min;
	}
	return -1;
}

int strToInt(string str)
{
	str += '\0';
	int value = 0;
	char numStr[100];
	strcpy(numStr, str.c_str());
	sscanf(numStr, "%d", &value);
	return value;
}

int main()
{
	string inputStr;
	int totalValue;
	cin >> inputStr >> totalValue;
	int len = inputStr.length();
	string temp = "";
	for (int i = 0; i < len; ++i)
	{
		if (inputStr[i] != ',')
		{
			temp += inputStr[i];
		}
		else
		{
			fruits.push_back(strToInt(temp));
			temp = "";
		}
	}
	if (temp != "")
	{
		fruits.push_back(strToInt(temp));
		temp = "";
	}
	int useCnt = 0;
	sort(fruits.begin(), fruits.end(), cmp);
	useCnt = change(totalValue, 0);
	printf("%d\n", useCnt);
	fruits.clear();

	//system("pause");
	return 0;
}

20%



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值