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 |
大牛指教一下,我直接用递归强行搜索,但超时,不知道该用什么方法#include<iostream> #include<string.h> #include<algorithm> using namespace std; int n; int untils[64]; int all; bool taken[64]; bool search(int start,int sum,int goal,int rest){ if(rest==0) return true; if(sum==goal) { return search(n-1,0,goal,rest-goal); } int i=start; for(;i>=0;i--){ if(taken[i]) continue; if(sum+untils[i] > goal) continue; taken[i]=true; bool answer=search(i-1,sum+untils[i],goal,rest); if(answer) return true; taken[i]=false; } return false; } int main() { cin>>n; while(n!=0){ all=0; for(int i=0;i<n;i++){ cin>>untils[i]; all+=untils[i]; } sort(untils,untils+n); for(int k=untils[n-1];k<=all;k++){ if(all%k==0){ memset(taken,0,sizeof(taken)); if(search(n-1,0,k,all)){ cout<<k<<endl; break; } } } cin>>n; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator