zoukankan      html  css  js  c++  java
  • The Triangle 分类: 动态规划 算法 2014-10-18 22:13 86人阅读 评论(0) 收藏

    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<stdio.h>
    
    int main(){
    	int n,i,j,len,sum,tem1,tem2;
    	int a[101][101],s[101][101];
    	scanf("%d",&n);
        len=n;
    	sum=0;
    
    		for(i=1;i<=len;i++){
    			for(j=1;j<=i;j++){
    				scanf("%d",&a[i][j]);
    			}
    		}
    		s[1][1]=a[1][1];
    		for(i=2;i<=len;i++){
    			for(j=1;j<=i;j++){
    				tem1=s[i-1][j-1]+a[i][j];
    				tem2=s[i-1][j]+a[i][j];
    				if(tem1>tem2){
    					s[i][j]=tem1;
    				}
    				else{
    					s[i][j]=tem2;
    				}
    			}
    		}
    		for(i=1;i<=len;i++)
    		{
    			if(s[len][i]>sum) sum=s[len][i];
    		}
    		printf("%d",sum);
    		
    	
    	return 0;
    
    
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    easyui 后台系统引入富文本编辑器的使用
    easyui datagrid 表格动态隐藏部分列的展示
    java ArrayList源码分析(转载)
    propertychange方法
    CSS margin-top 属性
    easyui-textbox input输入框的一种取值方式
    jquery next()方法
    jquery children()方法
    一段简单的表格样式
    常用的排序算法的时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/learnordie/p/4657001.html
Copyright © 2011-2022 走看看