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 denganliang at 2011-03-18 11:54:20 on Problem 1011
#include <stdio.h>
#include <stdlib.h>
int a[100];
int used[100];
int compare(const void *x,const void * y){
	return (int *)y-(int *)x;
	}
	
int fun(int n,int k,int left,int l){
	int i;
	if(k==0&&left==0) return 1;
    if(left==0) left=l;
    for(i=0;i<n;i++){
		if(a[i]>left) continue;
		if(used[i]==1) continue;
		used[i]=1;
		if(fun(n,k-1,left-a[i],l)==1) return 1;
		used[i]=0;
		if(a[i]==left||left==l) break;
		}
	return 0 ;
	}
int main(){
   	int n,flag=0,max,sum,i;
   	scanf("%d",&n);
   	while(n!=0){
		sum=0;
		
		for(i=0;i<n;i++){
			used[i]=0;
			scanf("%d",&a[i]);
			sum+=a[i];
			}
		qsort(a,n,sizeof(int),compare);
		max=a[0];
		for(i=max;i<=sum;i++)
		{
			if(sum%i!=0) continue;
			else {
				if(fun(n,n,0,i)==1){
					if(flag==1) printf("\n");
				    printf("%d",i);
				    break;
				  }
				  
				}
			}	
		flag=1;
		scanf("%d",&n);	
		}
	return 0;
	}
就是那个排序的时候停在了那组变态的数据哪里,有没有具体办法解决阿,由于是c的,所以不能用sort,只能用qsort,哪位大哥提个建议

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