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> using namespace std; int *stick=NULL; int *used=NULL; int ok; int length; int stickNum; int total; int n; int cmp ( const void *a , const void *b ) //´Ó´óµ½Ð¡ÅÅÐò { return *(int *)b - *(int *)a; } void match(int x); void search(int num,int nowLenth,int nextStick); int main() { while(cin>>n){ if(!n) break; ok=0; total=0; stick = new int[n+1]; used = new int[n+1]; int i; for( i = 1;i <= n;++i){ cin>>stick[i]; total += stick[i]; } qsort(stick+1,n,sizeof(int),cmp); for(i = stick[1];i <= total;++i){ if (total % i == 0 && !ok) { stickNum = total / i; memset(used,0,sizeof(used)); length = i; match(1); } } delete []stick; delete []used; stick=0; used=0; } return 0; } void match(int x)//Æ¥ÅäµÚX¸ù³¤°ô { if(x > stickNum){ ok = 1; cout<<length<<endl; return; } int i; for(i = 1;i <= n;++i) if(!used[i]) break; used[i] = 1; search(x,stick[i],i); used[i] = 0; } void search(int num,int nowLenth,int nextStick) { if(ok) return; if(nowLenth == length){ match(num + 1); return; } if(nextStick + 1 > n) return; for (int i = nextStick + 1;i <= n;i++){ if (!used[i]) if(stick[i] + nowLenth <= length) { used[i] = 1; search(num,nowLenth + stick[i],i); used[i] = 0; if (ok) return; if (stick[i] == length - nowLenth) return; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator