Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一下我交了20多次才A掉的代码,16MS#include<iostream> #define SIZE 70 using namespace std; int n,a[SIZE]; bool v[SIZE]; int cmp(void const * a,void const * b) { return *(int *)b-*(int *)a; } bool dfs(int i,int amount,int left,int len) { if(amount==0&&left==0) return true; else { if(left==len||left==0) left=len,i=0; else i++; for(;n-i>0;i++) { if(v[i]==false&&a[i]<=left) { if(i>0&&a[i-1]==a[i]&&v[i-1]==false) continue; v[i]=true; if(dfs(i,amount-1,left-a[i],len)) return true; v[i]=false; if(a[i]==left||len==left) return false; } } return false; } } int main() { while(scanf("%d",&n),n) { int sum=0; for(int i=0;n-i>0;i++) { scanf("%d",&a[i]); sum+=a[i]; } qsort(a,n,sizeof(int),cmp); bool ok=false; memset(v,false,sizeof(v)); for(int i=a[0];sum/2-i>=0;i++) { if(sum%i==0&&dfs(0,n,i,i)) { printf("%d\n",i); ok=true; break; } } if(ok==false) printf("%d\n",sum); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator