您的位置:首页 > 其它

The Triangle - 1163

2015-09-16 10:23 295 查看
The Triangle
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 41507Accepted: 25104
Description
7
3   8
8   1   0
2   7   4   4
4   5   2   6   5

(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.InputYour program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle,all integers, are between 0 and 99.OutputYour program is to write to standard output. The highest sum is written as an integer.Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Source
问题分析:
上述问题是一道典型的动态规划(DP)问题,动态规划和递归差不多,只不过比递归要难于理解,但在节省时间角度上,我们经常舍弃递归而采取动态规划的方法来处理问题。动态规划其实就是递推,下面我们来总结下什么问题适合递归,什么问题适合递推。对于一类问题,具有如下特征时,经常采取递归:
问题具有某种可借用的类同自身的子问题描述的性质;某一有限步的子问题(也称本原问题)有直接的解存在;最终有有结果返回给次外层。
注意:递归算法解题通常显得很简洁,但递归算法解题的运行效率较低。所以一般不提倡用递归算法设计程序。 在递归调用的过程当中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等。所以一般不提倡用递归算法设计程序。
通常我们对于一些问题会采用动态规划的递推来处理,比如下面这个问题:
对于连续输入的一串整数,找出它们当中和最大的一串子串(子串是连续的)。
eg:4,-5,3,2,-1,-3,5
其中最大的子串为(3,2,-1,-3,5),值为6。
算法思想:把问题分解为若干个小问题,每个小问题都可以用一个相同的算法求解,从而利用子问题的解来得到原问题的解。
子问题:从第一个数据开始分段,并把第一个数的值作为maxsum,来一个数就和前面的数加和,如果和不小于0就继续,然后进行和maxsum重新比较,如果大于maxsum就给maxsum赋新的值,如果不大于,还保留maxsum的值;如果来一个数和前面的数加和后小于0,则重新开始下一段。
代码如下:
从而,我们对于1163问题的解答代码如下:

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: