首页 > 其他 > 详细

数字三角形

时间:2015-08-06 23:56:57      阅读:342      评论:0      收藏:0      [点我收藏+]

题目描述 Description
如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得向右走,一直走到底层,要求找出一条路径,使路径上的值最大。
输入描述 Input Description
第一行是数塔层数N(1<=N<=100)。
第二行起,按数塔图形,有一个或多个的整数,表示该层节点的值,共有N行。
输出描述 Output Description
输出最大值。
样例输入 Sample Input
5
13
11 8
12 7 26
6 14 15 8
12 7 13 24 11
样例输出 Sample Output
86

var f:array[0..100,0..100] of longint;
    a:array[0..100,0..100] of longint;
    n:longint;
    i1,j1:longint;
    maxx:longint;
function max(x,y:longint):longint;
begin
  if x>y then exit(x)
  else exit(y);
end;
procedure shuzi(i,j:longint);
begin
  if i=n then f[i,j]:=a[i,j];
  if i>=n then exit;
  if f[i,j]=0 then exit;
  shuzi(i+1,j);
  shuzi(i+1,j+1);
  f[i,j]:=a[i,j]+max(f[i+1,j],f[i+1,j+1]);
end;
begin
  read(n);
  for i1:=1 to n do
   for j1:=1 to i1 do
    read(a[i1,j1]);
  fillchar(f,sizeof(f),char(-1));
  shuzi(1,1);
  writeln(f[1,1]);
end

 

数字三角形

原文:http://www.cnblogs.com/yangqingli/p/4709291.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!