nyoj 18 The Triangle
The Triangle
- 描述
-
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
- 5
- 样例输出
-
- 30
- #include <stdio.h>
- #include <algorithm>
- using namespace std;
- int main()
- {
- int n,maxx=0,num[105][105]= {0};
- scanf("%d",&n);
- for(int i=1; i<=n; i++)
- for(int j=1; j<=i; j++)
- scanf("%d",&num[i][j]);
- for(int i=2; i<=n; i++)
- {
- for(int j=1; j<=i; j++)
- {
- num[i][j]=num[i][j]+max(num[i-1][j],num[i-1][j-1]);
- }
- }
- for(int i=1; i<=n; i++)
- if(num[n][i]>maxx)
- maxx=num[n][i];
- printf("%d\n",maxx);
- return 0;
- }
- //#include<stdio.h>
- //#include<string.h>
- //#include<algorithm>
- //using namespace std;
- //
- //int n,dp[110][110],map[110][110];
- //
- //int dfs(int i,int j)
- //{
- // if(dp[i][j]!=-1)
- // return dp[i][j];
- // if(i==n) dp[i][j]=map[i][j];
- // else
- // {
- // int x=dfs(i+1,j);
- // int y=dfs(i+1,j+1);
- // dp[i][j]=max(x,y)+map[i][j];
- // }
- // return dp[i][j];
- //}
- //int main()
- //{
- // int i,j;
- // scanf("%d",&n);
- // for(i=1; i<=n; i++)
- // {
- // for(j=1; j<=i; j++)
- // {
- // scanf("%d",&map[i][j]);
- // dp[i][j]=-1;
- // }
- // }
- // printf("%d\n",dfs(1,1));
- // return 0;
- //}
- //#include <stdio.h>
- //#include <algorithm>
- //using namespace std;
- //int main()
- //{
- // int n,num[105][105]= {0};
- // scanf("%d",&n);
- // for(int i=1; i<=n; i++)
- // for(int j=1; j<=i; j++)
- // scanf("%d",&num[i][j]);
- // for(int i=n-1; i>=0; i--)
- // for(int j=1; j<=i; j++)
- // num[i][j]=num[i][j]+max(num[i+1][j],num[i+1][j+1]);
- // printf("%d\n",num[1][1]);
- // return 0;
- //}
- //
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- using namespace std;
- int n;
- int dp[110][110],map[110][110];
- int d(int i,int j)
- {
- if(dp[i][j]>=0) return dp[i][j];
- return dp[i][j]=map[i][j]+(i==n?0:(d(i+1,j)>d(i+1,j+1)?d(i+1,j):d(i+1,j+1)));
- }
- int main()
- {
- while(~scanf("%d",&n))
- {
- int i,j;
- for(i=1; i<=n; i++)
- {
- for(j=1; j<=i; j++)
- {
- scanf("%d",&map[i][j]);
- dp[i][j]=-1;
- }
- }
- int ans=d(1,1);
- printf("%d\n",ans);
- }
- return 0;
- }
nyoj 18 The Triangle的更多相关文章
- NYOJ 18 The Triangle 填表法,普通dp
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=18 The Triangle 时间限制:1000 ms | 内存限制:6553 ...
- leetcode 数组array
120. Triangle 给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上. 解法,自底向上 The idea is simple. Go from bot ...
- acdream.18.KIDx's Triangle(数学推导)
KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...
- nyoj 18-The Triangle(动态规划)
18-The Triangle 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:5 submit:5 题目描述: 7 3 8 8 1 0 2 7 4 ...
- NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏
地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...
- NYOJ-20 吝啬的国度 AC 分类: NYOJ 2014-01-23 12:18 200人阅读 评论(0) 收藏
#include<cstdio> #include<cstring> #include<vector> using namespace std; int pre[1 ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 设计一个程序,程序中有三个类,Triangle,Lader,Circle。
//此程序写出三个类,triangle,lader,circle:其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成 ...
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
随机推荐
- 重构改善既有代码设计--重构手法19:Replace Data Value with Object (以对象取代数据值)
你有一笔数据项(data item),需要额外的数据和行为. 将这笔数据项变成一个对象. class Order... private string customer; ==> class Or ...
- ASP.NET中防止Access数据库下载
如何防止Access数据库下载是一个很老的话题了,网上的讨论也比较多.这里我们给出几种在ASP.NET下防止Access数据库被下载的方法. 我们这里假设Access数据库名字为 test.mdb. ...
- windows下gitlab配置 生成ssh key
Git-1.9.5-preview20141217 1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C "e ...
- 谨慎重载clone方法
本文涉及到的概念 1.浅拷贝和深拷贝 2..clone方法的作用和使用方式 3.拷贝构造器和拷贝工厂 1.浅拷贝和深拷贝 浅拷贝 一个类实现Cloneable接口,然后,该类的实例调用clone方 ...
- Calendar 日期类介绍
Calendar c = Calendar.getInstance();//创建实例 默认是当前时刻 c.get(Calendar.YEAR); c.get(Calendar.MONTH); c.ge ...
- Attention-over-Attention Neural Networks for Reading Comprehension论文总结
Attention-over-Attention Neural Networks for Reading Comprehension 论文地址:https://arxiv.org/pdf/1607.0 ...
- 蓝色简单的cms文档管理系统模板——后台
链接:http://pan.baidu.com/s/1qYMwHis 密码:xyiw
- offset宏的讲解【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299584 1.offset宏讲解 #define offsetof(TYPE, MEMBER) ...
- .NET 处理视频-MediaInfo 获取视频信息
获取视频信息的组件很多,本节介绍的是:MediaFile. 第一步.添加 MediaInfoDotNet 在项目上右键,选择“管理 NuGet 程序包”,浏览以选中 MediaInfoDotNet,然 ...
- 大数据系列之Kafka安装
先简单说下安装kafka的流程..(可配置多个zookeeper,这篇文只说一个zookeeper场景) 1.环境配置:jdk1.7+ (LZ用的是jdk1.8) 2.资料准备:下载 kafka_2. ...