题目如下:
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 0Sample Output
6 5
这道题的思路是从最小约数开始找起,每找到一个约数后,用深搜的方法判断小棒们是否能够组合成这个约数的长度。这道题的关键在于深度搜索的剪枝,不剪枝很容易出现超时错误。
AC代码如下:
#include
#include
#include
#include
#include
using namespace std;
int number,stick[65];
bool used[65];
bool result = false;
int length;
bool cmp(int a, int b)
{
return a>b;
}
int find(int k,int culen)
{
//cout<length) && k=number)return;
//cout<<"k="<length && k=number)return;}
else {k = 0;while(used[k])k++;}
used[k]=true;
dfs(stick[k]+culen,k+1,usen+1);
used[k]=false;
if(culen==0)return;
int ked = k;
while(k=number)break;
if(stick[k-1]==stick[k] && !used[k-1])continue;
used[k] = true;
dfs(stick[k]+culen,k+1,usen+1);
ked = k;
used[k]=false;
}
return;
}
if(culen==length){
//cout<<"\tsuccess!"<
那么问题就来了, 上面的代码中主函数中的length变量是一个全局变量,在for循环里面它直接充当了控制变量,但是如果将这个for循环改成下面的样子就会出现WA。。
for(int i=stick[0]; i
真的特别迷...