题目链接

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 题意:有n段木棍,要求将这n根木棍拼成x跟长度相同的木棍,要使新的木棍尽量短,输出最小值? 思路:搜索,新的木棍的长度一定大于等于原来木棍的最大值maxlen,小于等于这些木棍的总长度sum,所以从小到大(maxlen~sum)遍历这些值,如果sum%i!=0 ,则肯定不能拼成合法的木棍直接跳过;
如果sum%i==0,那么有可能满足,则进行搜索。搜索过程:一根一根的深搜,记录当前已经拼成几根木棍,当前拼的这跟木棍还差多长,用过的木棍用v[i]进行标记。 代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int start,sum;
int v[]; int check(int num,int rest,int pos)
{
if(num==sum/start) return ;
rest-=pos; v[pos]--;
if(rest==)
{
int i;
for(i=; i>=; i--) if(v[i]) break;
int flag=check(num+,start,i);
v[pos]++;
return flag;
}
for(int i=rest; i>=; i--)
{
if(!v[i]) continue;
int flag=check(num,rest,i);
if(flag) return ;
}
v[pos]++;
return ;
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int maxn=-;
sum=;
memset(v,,sizeof(v));
for(int i=; i<=n; i++)
{
int x; scanf("%d",&x);
sum+=x;
v[x]++;
maxn=max(maxn,x);
}
for(start=maxn; start<=sum; start++)
{
if(sum%start!=) continue;
if(check(,start,maxn))
{
printf("%d\n",start);
break;
}
}
}
return ;
}
/**
9
21 16 33 36 19 1 35 6 47
ans=107 43
46 16 47 31 22 48 10 47 25 48 33 31 35 33 14 21 8 22 20 37 20 48 8 18 3 44 28 16 9 50 44 18 46 28 43 49 18 19 31 46 3 43 43
ans=141
*/ /**
20
4 2 1 6 6 5 6 9 1 0 6 9 0 4 8 3 2 1 1 6
20
6 8 9 4 5 10 6 5 8 5 5 7 9 6 3 10 3 1 9 9
*/

最新文章

  1. FFmpeg-for IOS 一[安装]
  2. Bootstrap的优先级、选择器、伪类
  3. MySQL 5.1.63 单机配置多实例(简单配置)
  4. phpStudy速度慢的解决办法
  5. No.4小白的HTML+CSS心得篇
  6. JavaScript运行命令
  7. 对&quot;一维最大子数组和&quot;问题的思考
  8. 关于APIcloud中的登录与注册的简单实现
  9. #pragma预处理命令
  10. 后台返回null iOS
  11. Postman-----Response body:JSON value check的使用介绍
  12. Sitecore8.2 Tracker.Current is not initialized错误
  13. UnityShader实现物体被遮挡描边
  14. LDAP2-创建OU创建用户
  15. Python开发——6.文件操作
  16. Dapper 连表查询
  17. 以太坊erc20转账失败的情况和原因
  18. Vue之vue自动化工具快速搭建单页项目目录
  19. Word 2010 小技巧篇
  20. 使 WPF 支持触摸板的横向滚动

热门文章

  1. 框架 之 -------Spring
  2. NYOJ--20--搜索(dfs)--吝啬的国度
  3. MySQL的grant,revoke使用
  4. 网站pv统计脚本
  5. css的一些问题
  6. python实战===使用随机的163账号发送邮件
  7. 推荐——基于python
  8. Linux内存详解
  9. Struts2传参碰到的奇怪问题
  10. 直播协议 HTTP-FLV 详解