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 windT at 2009-01-24 23:11:09 on Problem 1011
9
15 3 2 11 4 1 8 8 8
比如就这个数据。答案是20
但是我的程序会把20否定掉。
因为按照降序排列,20的搜索会是15+4+1,这样的话剩下的数据就无法得出20的结论
本来应该是
15+3+2
8+8+4
11+8+1
关键点是不知道怎么改。。谢谢
#include <iostream>
#define MAX 65
using namespace std;

int stick[MAX];
int used[MAX];
int n, nLen;
int sumL, num;
int yes;

int cmp(const void *r1, const void *r2)
{
	return *(int *)r2 - *(int *)r1;
}


int dfs(int nl, int ni, int tn)//nl:现在的长度;tn:现在已经搜索成功的棍子数目;
//ni:搜索的坐标,既要搜索的棍子坐标;
{
	int i;
	if(tn == num){yes = 1; return 1;}
	for(i=ni; i<n; i++)
	{
		if(!used[i])
			nl+=stick[i];
		if(nl>nLen)
		{
			nl-=stick[i];
			continue;
		}
	    else if(nl==nLen)
		{
			tn++;
			used[i]=1;
			if(dfs(0, 0, tn))return 1;
			used[i]=0;//??
		}
		else if(nl<nLen)
		{
			used[i]=1;
			ni=i+1;
			if(dfs(nl, ni, tn))return 1;
			used[i]=0;
			break;
		}
		if(nl==0)break;
	}
	return 0;
}

	
int main()
{
	int i;
	while(scanf("%d", &n) && n)
	{
		sumL = 0;
		memset(stick, 0, sizeof(stick));
		//Input
		for(i=0; i<n; i++)
		{
			scanf("%d", &stick[i]);
			sumL += stick[i];
		}
		//Sort from big to small
		qsort(stick, n, sizeof(int), cmp);
		for(i=stick[0]; ;i++)
		{
			yes = 0;
			if(sumL%i == 0)
			{
				memset(used, 0, sizeof(used));
				num = sumL/i;
				nLen = i;
				dfs(0, 0, 0);
    			if(yes == 1)break;
			}
		}
		printf("%d\n", nLen);
	}
	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