dfs 84分 求大佬帮忙
查看原帖
dfs 84分 求大佬帮忙
490748
cjihyy楼主2022/7/12 16:55

代码如下:

#include <bits/stdc++.h>
using namespace std;
int n,a[25],sum,res=1e9,height[25];
void dfs(int x,int b){
	if(x>n){
		if(sum>=b)res=min(res,sum);
		return;
	}
	a[x]=0;
	dfs(x+1,b);
	a[x]=1;
	sum+=height[x];
	dfs(x+1,b);
}
int main(){
	int b;
	cin>>n>>b;
	for(int i=1;i<=n;i++){
		cin>>height[i];
	}
	dfs(1,b);
	cout<<res-b;
	return 0;
}
2022/7/12 16:55
加载中...