#include<cstdio>
#include<vector>
using namespace std;
const int maxn=50;
int a[maxn],n,k,x,maxsumsqu=-1;
vector<int>temp,ans;
void dfs(int index,int nowk,int sum,int sumsqu){
if(nowk==k&&sum==x){
if(sumsqu>maxsumsqu){
maxsumsqu=sumsqu;//在每个分支中选举
ans=temp;
}
return ;//递归边界1(根据题目条件的边界)
}
if(index==n||nowk>k||sum>x) return;//递归边界2(枚举数量上已穷尽)
//选index号数字
temp.push_back(a[index]);
dfs(index+1,nowk+1,sum+a[index],sumsqu+a[index]*a[index]);
temp.pop_back();//此处是难点!!!!
//不选index号数字
dfs(index+1,nowk,sum,sumsqu);
}
int main(){
scanf("%d%d%d",&n,&k,&x);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
dfs(0,0,0,0);
for(int i=0;i<ans.size();i++){
printf("%d ",ans[i]);
}
printf("\n%d\n",maxsumsqu);
return 0;
}
dfs最后定的序列练习
最新推荐文章于 2020-07-08 04:48:49 发布