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

accept ?

Posted by sunnvya at 2006-09-14 19:37:36 on Problem 1011
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int a[64],b[64];
int m,s,f,w;
int compare(const void *i,const void *j)
{return *(int*)i-*(int*)j;}
void fun(int t, int now, int last) {
	if (f) return;
	if (t == w-1) {
		f = 1;
	} else if (now == s/w) {
		fun(t + 1, 0, 0);
	} else if (now < s/w) {
		for (int i = last + 1; i < m; ++i) 
			if (!b[i]) {
				b[i] = 1;
				fun(t, now + a[i], i);
				if (f) return;
				b[i] = 0;
			}
	}
}
int done()
{
    memset(b, 0, sizeof(b));
    f=0;
    b[0]=1;
    fun(0,a[0],0);
    return f;

}
int main()
{
	
        while(cin>>m&&m!=0)
	{
		s=0;//int a[m],b[m];
		for(int i=0;i<m;i++)
		{cin>>a[i];s+=a[i];}qsort(a,m,sizeof(int),compare);
		for(w=m;w>0;w--)
		{
			if(s%w==0)
			{
				done();
				if(done())break;

			}
		}
		cout<<s/w<<endl;
	}
	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