UESTC Training for Search Algorithm——L

Sticks

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

Source

Central Europe 1995

 

 

/*算法思想:
  我用DFS做的,先将所有的木棍从大到小排序,然后从最大的木棍开始枚举木棍能
  拼接的长度len,要是能拼接,退出循环,输出结果
  剪枝1:要是这跟木棍不能用来拼接,那么其他的和他长度相等的木棍也不能拼接
  剪枝2:不能找到一根长度为len的木棍,直接退出搜索,以后也不能找到了
*/

/*CODE*/
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 100
using namespace std;
int a[N],sum,n,len;
bool fg[80];
bool cmp(int a,int b)
{
    return a>b;
}
bool dfs(int v,int left,int tot)  //DFS,当前用到第v根木棍了,还剩下len凑够一根长度为len的木棍,总的木棍长度还剩tot
{
    if(tot==len) return true;  //如果总的木棍长度还剩tot,一定能够凑够
    for(int i=v;i<n;i++)
    if(!fg[i] && a[i]<=left)  //找一根能够用来拼凑的木棍
    {
        fg[i]=true;
        if(a[i]==left)
        {
            if(dfs(0,len,tot-a[i])) return true;  //找新的一根长度为len的木棍
        }
        else if(dfs(i+1,left-a[i],tot-a[i])) return true;
        fg[i]=false;
        if(a[i]==left) return false;  //不能找到一根新的长度为len的木棍,肯定不能找到了,返回false
        if(tot==sum) return false;  //用于第一次DFS,要是不能找到,返回false
        if(left==len) return false;  //不能找到一根长度为len的木棍,返回false
        while(a[i]==a[i+1]) i++;  //相同的木棍,上一个满足条件找了不能找到,这一个也一样的不能
    }
    return false;
}
int main()
{
    while(scanf("%d",&n),n!=0)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a,a+n,cmp);
        memset(fg,0,sizeof(fg));
        for(len=a[0];len<=sum;len++)
        {
            if(sum%len!=0) continue;
            if(dfs(0,len,sum)) { printf("%d\n",len); break; }
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值