程序设计思维与实践 Week3作业 A

题目:

Given n positive numbers, ZJM can select exactly K of them that sums to S. Now ZJM wonders how many ways to get it!

Input
The first line, an integer T<=100, indicates the number of test cases. For each case, there are two lines. The first line, three integers indicate n, K and S. The second line, n integers indicate the positive numbers.

Output
For each case, an integer indicate the answer in a independent line.

思路:

这一题的主要思路为dfs,即暴力枚举所有可能的情况,但需要做出一定的剪枝,在判断符合条件的情况下计数即可。这里我设计dfs函数的思路为对给定数据逐个判断是否选中,若选中则加入链表,且每对数据判断一次都要做以下判断:确认当前链表内数据个数是否超过题目给定个数,超过则回溯;确认链表内数据和是否大于题目需求和,大于则回溯;查看是否将给定数据判断完毕,判断完毕则回溯;若链表元素数及元素总和与题目给定相等,此时计数器加一,回溯。当该流程执行完毕后,计数器内得到的数就是最后的方案数。

代码:

#include <iostream>
#include <list>
#include <map>
using namespace std;
int k;int n;int s;int count=0;
int a[17];
map<list<int>,int> m;
void choose(int i,int sum,list<int> &li)
{
	if(li.size()==k&&sum==s/*&&m[li]==0*/)
	{
		count++;
		m[li]=1;
		return;
	}
	if(i>n||sum>s)	return;
	if(li.size()>k)  return;
	choose(i+1,sum,li);
	li.push_back(a[i-1]);
	choose(i+1,sum+a[i-1],li);
	li.pop_back();
	return;
}
int main()
{
	int t;
	cin>>t;
	list<int> li;
	for(int i=0;i<t;i++)
	{
		cin>>n>>k>>s; 
		for(int j=0;j<n;j++)
			cin>>a[j];
		choose(1,0,li);
		cout<<count<<"\n";
		count=0;
		li.clear();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值