码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1011 Sticks

时间:2015-12-03 09:51:37      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

这个问题是做哭= =
之前我们把所有变量定义为全局变量,结果总是提交的时间RT,我无法找到故障。后来改为局部变量ac该= =。

D - Sticks(3.4.2)
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

Input

The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.

Output

The output should contains the smallest possible length of original sticks, one per line.

Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

题目大意:

任意分割木棒,分割后想把这些碎木棒还原到原来状态,可是他忘记了原先有多少木棒以及那些木棒原来有多长。请计算原先木棒的最小可能长度。



#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int n,num[69];
int pd[69];
int cmp(int a,int b)
{
	return a>b;
}
int dfs(int len,int s,int nu)
{
	int i;
    if(s==0&&nu==0)//假设剩下的长度和剩下的木棒数为0。则成功
		return len;
    if(s==0)//假设剩下的长度为0,则又一次赋值
		s=len;
    for(i=0;i<n;i++)
    {
        if(pd[i]==1)
			continue;
        if(s>=num[i])
        {
            pd[i]=1;
            if(dfs(len,s-num[i],nu-1))
				return  len;	
			pd[i]=0;//假设不成功,则当前棒不使用
            if(num[i]==s||s==len)
				break;
            while(num[i]==num[i+1])
				i++;			
        }
    }
	return 0;
}
int main()
{
    while(cin>>n&&n)
    {
		int i;
		int sum;
        sum=0;
		int len,k;
		for(i=0;i<n;i++)
		{
			cin>>num[i];
			sum=sum+num[i];
		}
		sort(num,num+n,cmp);
		for(len=num[0];len<=sum;len++)
		{
			memset(pd,0,sizeof(pd));
			if(sum%len==0)
			{
				k=dfs(len,0,n);
				if(k)
					break;
			}
		}
		cout<<k<<endl;
    }
    return 0;
}
/*
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
*/



POJ 1011 Sticks

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5015017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!