蓝桥杯 ALGO-224 算法训练 Sticks(dfs+剪枝算法)

本题应用dfs(深度搜索算法)+剪枝算法,也是第一次接触这些算法,刚开始看这道题只以为遍历查找就好了,尝试很久之后发现并不可行,于是上网看了一些别人写的代码,也有了一些自己的理解


由于蓝桥杯选择了java,所以最近也在熟悉java语法,下面蓝桥杯习题也大多会用java进行编程


问题描述

  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.

输入格式

  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.

输出格式

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

样例输入

9

5 2 1 5 2 1 5 2 1

4

1 2 3 4

0

样例输出

5

6


剪枝通常应用在DFS 和 BFS 搜索算法中;剪枝策略就是寻找过滤条件,提前减少不必要的搜索路径。


代码如下(附注释):





测试结果如下:



嘻嘻嘻,今天就到这啦~~~

你可能感兴趣的:(蓝桥杯 ALGO-224 算法训练 Sticks(dfs+剪枝算法))