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.
样例输入
  1. 5
  2. 7
  3. 3 8
  4. 8 1 0
  5. 2 7 4 4
  6. 4 5 2 6 5
样例输出
  1. 30
  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4. int main()
  5. {
  6. int n,maxx=0,num[105][105]= {0};
  7. scanf("%d",&n);
  8. for(int i=1; i<=n; i++)
  9. for(int j=1; j<=i; j++)
  10. scanf("%d",&num[i][j]);
  11. for(int i=2; i<=n; i++)
  12. {
  13. for(int j=1; j<=i; j++)
  14. {
  15. num[i][j]=num[i][j]+max(num[i-1][j],num[i-1][j-1]);
  16. }
  17. }
  18. for(int i=1; i<=n; i++)
  19. if(num[n][i]>maxx)
  20. maxx=num[n][i];
  21. printf("%d\n",maxx);
  22. return 0;
  23. }
  24. //#include<stdio.h>
  25. //#include<string.h>
  26. //#include<algorithm>
  27. //using namespace std;
  28. //
  29. //int n,dp[110][110],map[110][110];
  30. //
  31. //int dfs(int i,int j)
  32. //{
  33. // if(dp[i][j]!=-1)
  34. // return dp[i][j];
  35. // if(i==n) dp[i][j]=map[i][j];
  36. // else
  37. // {
  38. // int x=dfs(i+1,j);
  39. // int y=dfs(i+1,j+1);
  40. // dp[i][j]=max(x,y)+map[i][j];
  41. // }
  42. // return dp[i][j];
  43. //}
  44. //int main()
  45. //{
  46. // int i,j;
  47. // scanf("%d",&n);
  48. // for(i=1; i<=n; i++)
  49. // {
  50. // for(j=1; j<=i; j++)
  51. // {
  52. // scanf("%d",&map[i][j]);
  53. // dp[i][j]=-1;
  54. // }
  55. // }
  56. // printf("%d\n",dfs(1,1));
  57. // return 0;
  58. //}
  59.  
  60. //#include <stdio.h>
  61. //#include <algorithm>
  62. //using namespace std;
  63. //int main()
  64. //{
  65. // int n,num[105][105]= {0};
  66. // scanf("%d",&n);
  67. // for(int i=1; i<=n; i++)
  68. // for(int j=1; j<=i; j++)
  69. // scanf("%d",&num[i][j]);
  70. // for(int i=n-1; i>=0; i--)
  71. // for(int j=1; j<=i; j++)
  72. // num[i][j]=num[i][j]+max(num[i+1][j],num[i+1][j+1]);
  73. // printf("%d\n",num[1][1]);
  74. // return 0;
  75. //}
  76. //
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. int n;
  7. int dp[110][110],map[110][110];
  8.  
  9. int d(int i,int j)
  10. {
  11. if(dp[i][j]>=0) return dp[i][j];
  12. 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)));
  13. }
  14.  
  15. int main()
  16. {
  17. while(~scanf("%d",&n))
  18. {
  19. int i,j;
  20. for(i=1; i<=n; i++)
  21. {
  22. for(j=1; j<=i; j++)
  23. {
  24. scanf("%d",&map[i][j]);
  25. dp[i][j]=-1;
  26. }
  27. }
  28. int ans=d(1,1);
  29. printf("%d\n",ans);
  30. }
  31. return 0;
  32. }

nyoj 18 The Triangle的更多相关文章

  1. NYOJ 18 The Triangle 填表法,普通dp

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=18 The Triangle 时间限制:1000 ms  |  内存限制:6553 ...

  2. leetcode 数组array

    120. Triangle 给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上. 解法,自底向上 The idea is simple. Go from bot ...

  3. acdream.18.KIDx's Triangle(数学推导)

    KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...

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

  5. 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很容易设计出一个基于二分的递归 ...

  6. NYOJ-20 吝啬的国度 AC 分类: NYOJ 2014-01-23 12:18 200人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> #include<vector> using namespace std; int pre[1 ...

  7. [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, ...

  8. 设计一个程序,程序中有三个类,Triangle,Lader,Circle。

    //此程序写出三个类,triangle,lader,circle:其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成 ...

  9. Think Python - Chapter 18 - Inheritance

    In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...

随机推荐

  1. 重构改善既有代码设计--重构手法19:Replace Data Value with Object (以对象取代数据值)

    你有一笔数据项(data item),需要额外的数据和行为. 将这笔数据项变成一个对象. class Order... private string customer; ==> class Or ...

  2. ASP.NET中防止Access数据库下载

    如何防止Access数据库下载是一个很老的话题了,网上的讨论也比较多.这里我们给出几种在ASP.NET下防止Access数据库被下载的方法. 我们这里假设Access数据库名字为 test.mdb. ...

  3. windows下gitlab配置 生成ssh key

    Git-1.9.5-preview20141217 1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C "e ...

  4. 谨慎重载clone方法

    本文涉及到的概念 1.浅拷贝和深拷贝 2..clone方法的作用和使用方式 3.拷贝构造器和拷贝工厂   1.浅拷贝和深拷贝 浅拷贝 一个类实现Cloneable接口,然后,该类的实例调用clone方 ...

  5. Calendar 日期类介绍

    Calendar c = Calendar.getInstance();//创建实例 默认是当前时刻 c.get(Calendar.YEAR); c.get(Calendar.MONTH); c.ge ...

  6. Attention-over-Attention Neural Networks for Reading Comprehension论文总结

    Attention-over-Attention Neural Networks for Reading Comprehension 论文地址:https://arxiv.org/pdf/1607.0 ...

  7. 蓝色简单的cms文档管理系统模板——后台

    链接:http://pan.baidu.com/s/1qYMwHis 密码:xyiw

  8. offset宏的讲解【转】

    转自:http://blog.csdn.net/tigerjibo/article/details/8299584 1.offset宏讲解 #define offsetof(TYPE, MEMBER) ...

  9. .NET 处理视频-MediaInfo 获取视频信息

    获取视频信息的组件很多,本节介绍的是:MediaFile. 第一步.添加 MediaInfoDotNet 在项目上右键,选择“管理 NuGet 程序包”,浏览以选中 MediaInfoDotNet,然 ...

  10. 大数据系列之Kafka安装

    先简单说下安装kafka的流程..(可配置多个zookeeper,这篇文只说一个zookeeper场景) 1.环境配置:jdk1.7+ (LZ用的是jdk1.8) 2.资料准备:下载 kafka_2. ...