标签:style blog io color sp for div log bs
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int n,tot,ans; 6 bool v[100]; 7 int a[100]; 8 bool cmp(int x,int y) 9 { 10 return x>y; 11 } 12 bool dfs(int now,int dep,int ans,int len) 13 { 14 if (ans==(tot/len)) return true; 15 else if (now>len) return false; 16 else if (now==len) return dfs(0,1,ans+1,len); 17 else for (int i=dep;i<=n;i++) 18 { 19 if (v[i]!=1) 20 { 21 v[i]=1; 22 if (dfs(now+a[i],i+1,ans,len)) return true; 23 v[i]=0; 24 if (now==0) return false; 25 } 26 } 27 return false; 28 } 29 int main() 30 { 31 scanf("%d",&n); 32 while (n!=0) 33 { 34 tot=0; 35 memset(v,0,sizeof(v)); 36 for (int i=1;i<=n;i++) 37 { 38 scanf("%d",&a[i]); 39 tot+=a[i]; 40 } 41 sort(a+1,a+n+1,cmp); 42 for (int len=a[1];len<=tot;len++) 43 if (tot%len==0) 44 { 45 ans=0; 46 if (dfs(0,1,0,len)) 47 { 48 printf("%d\n",len); 49 break; 50 } 51 } 52 53 scanf("%d",&n); 54 } 55 return 0; 56 }
标签:style blog io color sp for div log bs
原文地址:http://www.cnblogs.com/NicoleLam/p/4164221.html