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

大家帮忙看下,几乎同样的程序c++ AC ,c 就TLE,附源码,大家帮我看看哪里有问题吧。谢谢啦!

Posted by Nowitzki at 2007-05-08 17:14:15 on Problem 1011
Source of C++:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int n; 
int stick[100]; 
int total; 
int ns; 
int ok; 
int len; 

int cmp(const void *a,const void *b) { 
   int a1 = *(int *)a; 
   int a2 = *(int *)b; 
   return a2 - a1; 
} 
int used[100]; 

int adds() { 
   int j = 0; 
   for (int i = 1;i <= n;i++) 
      j += stick[i]; 
   return j; 
} 

void search(int,int,int); 

void s(int x) { 
   if (x > ns) { 
      ok = 1; 
      printf("%d\n", len); 
      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 now,int next) { 

   if (ok) return; 
   if (now == len) { 
       s(num + 1); 
    return; 
   } 

   if (next + 1 > n) return; 

   for (int i = next + 1;i <= n;i++) 
      if (!used[i]) 
       if(stick[i] + now <= len) { 
         used[i] = 1; 
         search(num,now + stick[i],i); 
         used[i] = 0; 
         if (ok) return; 
         if (stick[i] == len - now) return;  
       } 
} 

int main () { 
   while (scanf("%d", &n) == 1) { 
       if (!n) break; 
       ok = 0; 
        
    int i; 
    for (i = 1;i <= n;i++) scanf("%d", &stick[i]); 
    qsort(stick+1,n,sizeof(int),cmp); 
    total = adds(); 
    for (i = stick[1];i <= total;i++) 
        if (total % i == 0 && !ok) { 
            ns = total / i; 
            memset(used,0,sizeof(used)); 
            len = i; 
            s(1); 
        } 
   } 
   return 0; 
} 




Source of C:

/***********POJ 1011 Sticks********************/
/****************Searching*********************/
/**********************Nowitzki May 8 2007*****/


#include<stdio.h>
#include<stdlib.h>

int n, stick[100], sum, length, used[100], f, num;

void search( int, int, int );

void preOperate( )
{
	int i;
	sum = 0;
	f = 0;
	for( i = 0; i < n; i++ )
	{
		sum += stick[i];
		used[i] = 0;
	}
}

int compare( const void *a, const void *b )
{
	return *( int * )a - *( int * )b;
}

void sort( )
{
	qsort( stick, n, sizeof( int ), compare );
}

void solve( int x )
{
	int i;

	if( x > num ) {
		f = 1;
		printf( "%d\n", length );
		return;
	}

	for( i = 0; i < n; i++ )
		if( !used[i] ) break;

	used[i] = 1;
	search(	x, stick[i], i );
	used[i] = 0;
}

void search( int cur, int curLength, int next )
{
	int i;
	if( f ) return;
	if( curLength == length ) {
		solve( cur + 1 );
		return ;
	}
	if( next + 1 == n ) return;
	for( i = next + 1; i < n; i++ )
		if( !used[i] && curLength + stick[i] <= length ) {
			used[i] = 1;
			search( cur, curLength + stick[i], i );
			used[i] = 0;
			if( f ) return;
			if( stick[i] == length - curLength ) return;
		}
}

int main( void )
{
	int i;
	scanf( "%d", &n );
	while( n != 0 )
	{
		for( i = 0; i < n; i++ )
			scanf( "%d", &stick[i] );
		preOperate( );
		sort( );
		for( i = stick[0]; i <= sum; i++ )
			if( sum % i == 0 && f == 0 ) {
				num = sum / i;
				length = i;
				solve( 1 );
				if( f == 1 ) break;
			}
		scanf( "%d", &n );
	}
	return 0;
}

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