• 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;
    
    
    }


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

  • 相关阅读:
    第六课 3. 外部表
    第六课 2 物化视图
    第六课 1.当有数据文件被误删除时如何恢复
    SQL常用(通用)操作_01
    SQL规范
    C# foreach和for比较
    C# 装箱与拆箱
    C#面向对象笔记
    winform防止输入法对扫码的干扰
    GIT安装包备用地址
  • 原文地址:https://www.cnblogs.com/learnordie/p/4657001.html
Copyright © 2020-2023  润新知