南阳理工oj_The Triangle
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的更多相关文章
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- 矩形嵌套 南阳理工ACM
描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度).例如(1, ...
- 单调递增最长子序列(南阳理工ACM)
描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0<n<20,表示有n个字符串要处理随后的n行,每行有一个字符串,该字符串 ...
- 南阳理工ACM 括号匹配问题,并求出使得括号能够匹配需要新增的最小括号数(括号匹配(二))
描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起 ...
- 南阳理工ACM Skiing问题
描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底 ...
- 南阳理工ACM1076--方案数量
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1076 分析: <span style="font-size:18px;& ...
- 南阳理工oj88--汉诺塔(一)
题目链接.http://acm.nyist.net/JudgeOnline/problem.php?pid=88 #include <stdio.h> /* //测试一下49999和500 ...
- 南阳理工ACM954--N!
http://acm.nyist.net/JudgeOnline/problem.php?pid=954 循环的可怕之处!! 所有的测试数据结果完全一样.只是超时!!TimeLimitExceeded ...
- 南阳理工ACM975--关于521
http://acm.nyist.net/JudgeOnline/problem.php?pid=975 这是我的源码.一直超时,一直超时. 还有itoa函数函数的使用.可以改成sprintf(str ...
随机推荐
- Java I/O---IO流的规律小结
IO流的规律总结:解决的问题,就是开发中具体要使用哪个流对象的问题. 1,明确数据源,数据汇(数据目的) 其实就是在明确要使用的IO体系:字节流 InputStream & OutputStr ...
- mac下配置caffe
Step1:安装homebrew 如果电脑上有,暂时不装.但是在step2(或者其他需要brew的情况)加完sudo之后如果仍然报错,就需要重新安装homebrew.在终端里输入如下命令: ruby ...
- 某次送温暖考试的 c题
题目大意: 给定n个点的无根树,树上每个点都有一个非负的点权. 树上的路径的价值定义为树上路径的点权和-树上路径的点权最大值; 现在给定一个参数P询问有多少条路径的价值是P的倍数(注意单点也算路径,路 ...
- Thinkphp开启调试模式
3.0版本的调试模式开启,必须在项目入口文件中添加常量APP_DEBUG定义,如下: define('APP_DEBUG',True); // 开启调试模式 开启调试模式后,你可能感觉不到什么变化,不 ...
- [摘抄]VC6.0移植到VS2008(vs2005)后的错误总结(未全部验证)
============================================================================================= 201405 ...
- 关于php中,记录日志中,将数组转为json信息记录日志时遇到的问题总结
1 中文编码化,无法看到具体的中文,如:你好 => \u4F60\u597D 解决方案:可以使用 json_encode($arr,JSON_UNESCAPED_UNICODE) 转义中文[ ...
- thinkphp 官方文件执行引入流程
官方手册上的执行流程图: 系统流程 用户URL请求 调用应用入口文件(通常是网站的index.php) 载入框架入口文件(ThinkPHP.php) 记录初始运行时间和内存开销 系统常量判断及定义 载 ...
- vue-router源码学习(一)
因为v3.01版本中的 /src代码使用TypeScript进行书写,我这里仅仅用作模块学习, 具体学习的还是 /dist/vue-router.js 代码. (一)基本使用方式 JS代码 // ...
- bash脚本之数组学习
在bash中可使用索引数组和关联数组,bash在4.0版本之后才添加了对关联数组的支持 一.索引数组 1.定义索引数组 # 方式1 array_value=(1 2 3 4 5 6)或者array_v ...
- 第七章:Python基础のXML操作和面向对象(一)
本課主題 XML介绍与操作实战 shutil 模块介绍与操作实战 subprocess 模块介绍与操作实战 初探面向对象与操作实战 本周作业 XML介绍和操作实战 對於浏览器返回的字符串有以下幾種: ...