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 |
Re:为什么我的是 Run time errorIn Reply To:为什么我的是 Run time error Posted by:INMCS at 2010-05-10 09:54:54 请高手帮我看看,小弟刚刚接触,妄请指教! #include <iostream> using namespace std; int n; // sticks 的个数 int sticks[100]; int used[100]; int len; int lenofsticks; int sum; bool ready=false; void DFS(int index,int currentLength,int numofStick) { int i,j,k; for(i=index+1;i<n;i++) { if(sticks[i]+currentLength<len && !used[i]) { used[i]=true; DFS(i+1,sticks[i]+currentLength,numofStick); used[i]=false; if(currentLength==0)//如果最长的木棍搜不出来,就直接退出,或者如果当前棒刚好填满并且失败了,也退出 { break; } while(i+1<n&&sticks[i+1]==sticks[i])//跳过相同长度的木棒 { i++; } } else if(sticks[i]+currentLength==len && !used[i]) { if (numofStick+1==lenofsticks) { ready=true; break; } else { used[i]=true; DFS(0,0,numofStick+1); used[i]=false; } break; } } } int compare(const void *a,const void *b) { return *((int*)b)-*((int*)a); } int main() { int i=0,j=0,k=0; while(scanf("%d",n)) { if(n==0) break; for(i=0;i<n;i++) { scanf("%d",sticks[i]); sum+=sticks[i]; } qsort(sticks,n,sizeof(sticks[0]),compare); for(len=sticks[0];len<=sum;len++) { if(sum%len==0) { memset(used,0,100); lenofsticks=sum/len; DFS(0,0,0); if(ready) break; } } printf("%d",len); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator