George took sticks of the same length and cut them randomly until all parts became at most 50 unitslong. Now he wants to return sticks to the original state, but he forgot how many sticks he had originallyand how long they were originally. Please help him and design a program which computes the smallestpossible original length of those sticks. All lengths expressed in units are integers greater than zero.InputThe input file contains blocks of 2 lines. The first line contains the number of sticks parts after cutting.The second line contains the lengths of those parts separated by the space. The last line of the filecontains ‘0’.OutputThe output file contains the smallest possible len#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k,len;
int num[100010],book[100010];
int cmp(int x,int y)
{
return x>y;
}
int dfs(int cnt,int p,int j)
{
if(cnt==len)
{
cnt=0;
p++;
j=0;
}
if(p==k)
{
return 1;
}
for(int i=j+1;i<n;i++)
{
if(!book[i]&&cnt+num[i]<=len)
{
if(!book[i-1]&&num[i]==num[i-1])continue;
book[i]=1;
if(dfs(num[i]+cnt,p,i))return 1;
book[i]=0;
if(cnt==0)return 0;
}
}
return 0;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(book,0,sizeof(book));
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&num[i]);
sum+=num[i];
}
sort(num,num+n,cmp);
for(len=num[0];len<=sum/2;len++)
{
if(sum%len==0)
{
book[0]=1;
k=sum/len;
if(dfs(num[0],0,0))break;
}
}
if(len<=sum/2)printf("%d\n",len);
else printf("%d\n",sum);
}
}
gth of original sticks, one per line.Sample
Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
//有n个木棍,段成许多部分,每根木棍的长度是一样的,要找出原来最短是多少根木棍,要利用深搜的减值
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k,len;
int num[100010],book[100010];
int cmp(int x,int y)
{
return x>y;
}
int dfs(int cnt,int p,int j)
{
if(cnt==len)
{
cnt=0;
p++;
j=0;
}
if(p==k)
{
return 1;
}
for(int i=j+1;i<n;i++)
{
if(!book[i]&&cnt+num[i]<=len)
{
if(!book[i-1]&&num[i]==num[i-1])continue;
book[i]=1;
if(dfs(num[i]+cnt,p,i))return 1;
book[i]=0;
if(cnt==0)return 0;
}
}
return 0;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(book,0,sizeof(book));
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&num[i]);
sum+=num[i];
}
sort(num,num+n,cmp);
for(len=num[0];len<=sum/2;len++)
{
if(sum%len==0)
{
book[0]=1;
k=sum/len;
if(dfs(num[0],0,0))break;
}
}
if(len<=sum/2)printf("%d\n",len);
else printf("%d\n",sum);
}
}