Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请教各位牛人下,为什么我改成动态数组后,就错了咯!

Posted by just_living at 2007-04-23 00:43:45 on Problem 1011
原来开了下静态的数组,通过了,后来改成动态的竟然不行了,各位指教下,什么地方用错了,谢了咯!



#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 ) //&acute;&Oacute;&acute;ó&micro;&frac12;&ETH;&iexcl;&Aring;&Aring;&ETH;ò
{ 
     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)//&AElig;&yen;&Aring;&auml;&micro;&Uacute;X&cedil;ù&sup3;¤°&ocirc;
{
	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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator