A - 选数问题(递归实现dfs)

选数问题

  • 题意:Given nn positive numbers, ZJM can select exactly KK of them that sums to SS. Now ZJM wonders how many ways to get it!

  • 输入输出
    Input:The first line, an integer T<=100T<=100, indicates the number of test cases. For each case, there are two lines. The first line, three integers indicate nn, KK and SS. The second line, nn integers indicate the positive numbers.

    output:For each case, an integer indicate the answer in a independent line.
    在这里插入图片描述

  • 解题思路
    选数时需要进行dfs,本题利用递归实现。定义三个变量分别记录当前数的位置,当前所选数的个数以及已选数的和,从(1,0,0)开始递归,每次先进行判断,如果所选数和已选数的和都等于要求时结束函数,计数器加一,计数器记录满足条件的情况。

    同时需要及时进行剪枝优化程序,即当所选数数目超过要求或已选数的和已经大于要求的和,及时结束本次函数调用,返回上一层,继续向下递归。当第一层dfs调用完毕,计数器存储值即为方案数。

  • 拓展延伸
    如果要输出方案,可以新开一个数组记录每个方案中所选的数,利用vector较为方便,要选的数加入在数组尾端不选的删除(push_back和pop_back),递归完成后输出方案即可。

  • 代码实现

#include <iostream> 
#include <cstring>
using namespace std;

int n,num,limit,ans,p;
int a[2000];

void dfs(int x,int y,int z)//x:数组中第x个数,y:已选数的个数,z:已选数的和 
{
	if(y == limit && z == ans) 
	{
		p++;	
		return;		
	}
	
	if(y > limit || z > ans)
		return;
		
	for(int i=x ;i<=num; i++)
	{
		//cout<<a[i]<<endl;
		dfs(i+1,y+1,z+a[i]);
	}
		
}

int main()
{
	cin>>n;
	while(n--)
	{
	    cin>>num>>limit>>ans;
		memset(a,0,sizeof(a) );
		for(int i=1;i<=num;i++)
			cin>>a[i];
		p=0;
		dfs(1,0,0);
		cout<<p<<endl;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值