P1164 小A点菜(搜索)

题目描述

在这里插入图片描述
在这里插入图片描述

代码

#include<bits/stdc++.h>
using namespace std;
int n,m,a[105],ans = 0;
void dfs(int n,int m){
    
    
	if(m == 0){
    
    
		ans++;
		return ;
	}
	if(n < 0 || m < 0)	return ;
	//如果倒数第n样菜拿的话 
	dfs(n-1,m-a[n]);
	//如果倒数第n样菜不拿的话 
	dfs(n-1,m);	
}
int main(){
    
    
	cin>>n>>m;	//总共M元 
	for(int i = 1;i<=n;i++){
    
    
		cin>>a[i];
	}
	dfs(n,m);
	cout<<ans<<endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/m0_45210226/article/details/108234730