The Triangle--nyoj 18
来源:互联网 发布:什么是sql数据库系统 编辑:程序博客网 时间:2024/05/17 07:35
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.
- 样例输入
573 88 1 0 2 7 4 44 5 2 6 5
- 样例输出
30
其实就是一个简单的dp思想,每一步都去了最优值从而达到整体最优
代码
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[1010][1010],i,j,n,sum;int main(){memset(a,0,sizeof(a));sum=0;scanf("%d",&n);for(i=1;i<=n;i++){for(j=1;j<=i;j++)scanf("%d",&a[i][j]);}for(i=n-1;i>=1;i--){for(j=1;j<=i*2-1;j++){a[i][j]=max(a[i][j]+a[i+1][j],a[i][j]+a[i+1][j+1]);}}printf("%d\n",a[1][1]);return 0;}
0 0
- NYOJ 18 The Triangle
- NYOJ 18-The Triangle
- The Triangle(nyoj 18)
- nyoj-18-The Triangle
- NYOJ-18 The Triangle
- NYOJ 18 The Triangle
- The Triangle--nyoj 18
- nyoj 18The Triangle
- NYOJ 18 The Triangle
- NYOJ-18 The Triangle
- nyoj 18 The Triangle
- NYOJ 18 The Triangle
- NYOJ 题目18 The Triangle
- NYOJ 题目18 The Triangle
- nyoj 18 The Triangle 动态规划
- nyoj 18 The Triangle 【经典dp】
- NYOJ 18 The Triangle(简单dp)
- nyoj 18 The Triangle(数塔)
- cocos2d-x 接入android ,登录第三方SDK时屏幕不断闪烁刷屏的问题解决
- [leetcode]Power of Two C语言
- 酷划邀请码,酷划官方邀请码
- Java开发中的23种设计模式详解(转)
- 字符数组与字符指针
- The Triangle--nyoj 18
- 零基础学python-12.5 修改列表的误区以及使用for和range修改列表
- Linux NTP配置详解 (Network Time Protocol)
- 11gR2 Agent 简介
- javascript Object Pool 对象池
- Static 关键字在 C 语言中的作用
- Android App 性能优化实践
- 更改UIImage颜色
- rejected execution (queue capacity 50)