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 - 样例输出
-
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 ...
随机推荐
- IDEA不显示更新、提交按钮
问题描述: IDEA右上角不显示版本管理的“更新”.“提交”等按钮,左侧代码树中,也没有文件的状态 解决办法: 点击VCS-->Enable Version Control Integratio ...
- python学习笔记(十三)之lambda表达式
lambda表达式: 用法 lambda x : 2 * x + 1 其中:前面是参数,后面是返回值. >>> def ds(x): ... return 2 * x + 1 ... ...
- C语言二分查找
#include <stdio.h> /* 二分查找条件: 1.有序序列 2.数据在数组中 */ int baseBinarySearch(int a[],int h,int k) { ; ...
- 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)
题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...
- 爬虫--Scrapy框架的基本使用
流程框架 安装Scrapy: (1)在pycharm里直接就可以进行安装Scrapy (2)若在conda里安装scrapy,需要进入cmd里输入指令conda install scrapy ...
- mysql数据库单表增删改查命令
数据库DB-database-mysql 课程安排 第一天: 1.数据库定义以及设计 2.mysql服务端的安装 3.mysql-dos操作 库的操作 表的操作 4.mysql客户端navicate工 ...
- PHP7+Nginx的配置与安装教程详解
下面脚本之家小编把PHP7+Nginx的配置与安装教程分享给大家,供大家参考,本文写的不好还请见谅. 系统环境:centos6.5 x64 软件版本:nginx-1.10.0 php-7.0.6 安装 ...
- 【技术分享】ReBreakCaptcha:利用谷歌来破解谷歌的验证码
概述 从2016年开始,我就在琢磨寻找一种新的绕过谷歌验证码v2的方法会有多难,如果这种方法能够适用于任何环境而不仅仅是针对特定的案例,那这种方法将是非常理想的.接下来我将向你介绍ReBreakCap ...
- 32.Longest Valid Parentheses---dp
题目链接:https://leetcode.com/problems/longest-valid-parentheses/description/ 题目大意:找出最长的括号匹配的子串长度.例子:&qu ...
- Mac 终端自动补全忽略大小写
打开终端,输入:nano .inputrc 在里面粘贴上以下语句: set completion-ignore-case onset show-all-if-ambiguous onTAB: menu ...