标签:
5 5 8 13 27 14
3
1 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 7 const int inf=999999; 8 bool flag; 9 int temp; 10 11 int sum,n,ans; 12 int used[21],a[21]; 13 14 void dfs(int cur,int total,int num) 15 { 16 if(cur==num) 17 return; 18 int temp=abs(sum-2*total); 19 ans=min(ans,temp); 20 if(temp==0||(total>=sum/2&&temp<=ans)) 21 return; 22 dfs(cur+1,total+a[cur],num); 23 dfs(cur+1,total,num); 24 } 25 26 int main() 27 { 28 //freopen("in.txt","r",stdin); 29 int i; 30 while(scanf("%d",&n)!=EOF) 31 { 32 ans=inf; 33 for(sum=0,i=0;i<n;i++) 34 { 35 scanf("%d",&a[i]); 36 sum+=a[i]; 37 } 38 memset(used,0,sizeof(used)); 39 dfs(0,0,n); 40 printf("%d\n",ans); 41 } 42 return 0; 43 } 44
标签:
原文地址:http://www.cnblogs.com/homura/p/4696633.html