给大家推荐个靠谱的公众号程序员探索之路,大家一起加油
#include<stdio.h>
#include<math.h>
int min,sum,total,n;
int weight[25];
void dfs(int c,int sum,int a[]){
int t;
if(c==n) return;
t=(int)fabs(total-sum-sum);
if(t<min) min=t;
dfs(c+1,sum,a);
dfs(c+1,sum+a[c],a);
}
int main(){
int i;
while(scanf("%d",&n)!=EOF){
total=0;
for(int i=0;i<n;i++){
scanf("%d",&weight[i]);
total+=weight[i];
}
min=200001;
dfs(0,0,weight);
printf("%d\n",min);
}
return 0;
}