The Triangle

时间限制:1000 ms | 内存限制:65535 KB

难度:4

描述

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.

输入

Your 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.

输出

Your program is to write to standard output. The highest sum is written as an integer.

样例输入

5

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

样例输出

30

上传者

苗栋栋

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std; int a[110][110], dp[110][110];
#define mem(a) memset(a, 0, sizeof(a)) int main() {
int n;
while (cin >> n) {
mem(a); mem(dp);
for (int i = 0; i<n; i++) {
for (int j = 0; j<=i; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i<n; i++) dp[n-1][i] = a[n-1][i];
for (int i = n-2; i>=0; i--) {
for (int j = i; j>=0; j--) {
dp[i][j] = max(a[i][j] + dp[i+1][j], a[i][j] + dp[i+1][j+1]);
}
}
cout << dp[0][0] << endl;
}
return 0;
}

南阳理工oj_The Triangle的更多相关文章

  1. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

  2. 矩形嵌套 南阳理工ACM

    描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度).例如(1, ...

  3. 单调递增最长子序列(南阳理工ACM)

    描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0<n<20,表示有n个字符串要处理随后的n行,每行有一个字符串,该字符串 ...

  4. 南阳理工ACM 括号匹配问题,并求出使得括号能够匹配需要新增的最小括号数(括号匹配(二))

    描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起 ...

  5. 南阳理工ACM Skiing问题

    描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底 ...

  6. 南阳理工ACM1076--方案数量

    题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1076 分析: <span style="font-size:18px;& ...

  7. 南阳理工oj88--汉诺塔(一)

    题目链接.http://acm.nyist.net/JudgeOnline/problem.php?pid=88 #include <stdio.h> /* //测试一下49999和500 ...

  8. 南阳理工ACM954--N!

    http://acm.nyist.net/JudgeOnline/problem.php?pid=954 循环的可怕之处!! 所有的测试数据结果完全一样.只是超时!!TimeLimitExceeded ...

  9. 南阳理工ACM975--关于521

    http://acm.nyist.net/JudgeOnline/problem.php?pid=975 这是我的源码.一直超时,一直超时. 还有itoa函数函数的使用.可以改成sprintf(str ...

随机推荐

  1. Intellij IDEA 像eclipse那样给maven添加依赖

    打开pom.xml,在它里面使用快捷键:ALT+Insert  ---->点击dependency 再输入想要添加的依赖关键字,比如:输个spring   出现下图: 根据需求选择版本,完成以后 ...

  2. Wincc flexable的画面浏览切换组态

    1.新建项目和6个画面 2.双击导航控件设置,选择默认设置 3.使用画面浏览编辑器编辑画面层次切换关系,拖拽画面到编辑器中进行关系连接 4.保运并运行

  3. MySQL 使用经验

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/10 在索引中完成排序 SELECT thread_id FROM ...

  4. 童话故事 --- 什么是SQL Server Browser

    高飞狗这几天特别郁闷,不知该如何通过TCP/IP协议连接SQL Server数据库.好在功夫不负有心人,经过几天的刻苦研究,终于得到了答案. 高飞狗呼叫UDP1434端口,"叮铃铃,叮铃铃- ...

  5. Linux发行版 CentOS6.5下删除分区操作

    本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢!   有时候,发现分区分错了.需要删除分区,只需按照分区的步骤逆向操作 ...

  6. Git详解之六:Git工具

    Git 工具 现在,你已经学习了管理或者维护 Git 仓库,实现代码控制所需的大多数日常命令和工作流程.你已经完成了跟踪和提交文件的基本任务,并且发挥了暂存区和轻量级的特性分支及合并的威力.(伯乐在线 ...

  7. JavaScript的DOM编程--02--获取元素节点

    如何来获取元素节点: 1) .document.getElementById: 根据 id 属性获取对应的单个节点 2) .document.getElementsByTagName: 根据标签名获取 ...

  8. 微信小程序参数二维码6问6答

    微信小程序参数二维码[基础知识篇],从6个常见问题了解小程序参数二维码的入门知识. 1.什么是小程序参数码? 微信小程序参数二维码:针对小程序特定页面,设定相应参数值,用户扫描后进入相应的页面. 2. ...

  9. Linux文件的复制、删除和移动命令

    cp命令  功能:将给出的文件或目录拷贝到另一文件或目录中,就如同DOS下的copy命令一样,功能非常强大.  语法:cp [选项] 源文件或目录 目标文件或目录  说明:该命令把指定的源文件复制到目 ...

  10. <转>LOG日志级别

    Level Description Example emerg Emergencies - system is unusable 紧急 - 系统无法使用 Child cannot open lock ...