拔河比赛
解题思路
深搜
设x为考虑到第x个成员
设sum为已经选择了sum个人
设ans为体重和
AC代码
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int n,s,answer,w[25];
void dfs(int x,int sum,int ans)//dfs
{
if(sum==n/2)
{
answer=min(answer,abs(ans*2-s));
return;
}
if(x>n)return;
dfs(x+1,sum+1,ans+w[x]);
dfs(x+1,sum,ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
s=0;//清零
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]),s+=w[i];
answer=s;//初值
dfs(1,0,0);
printf("%d\n",answer);
}
return 0;
}