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


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

  • 相关阅读:
    【FPGA】结构化层次化设计
    【FPGA】库、包和子程序 过程 函数
    【FPGA】8位奇偶校验 / 加(减)法器 VHDL程序
    【FPGA】顺序语句
    【FPGA】并行语句
    【FPGA】VHDL基础
    Luogu P3349 小星星
    Luogu P4284 概率充电器
    NOIP2018 | D2T2 & D2T3
    二项式反演 小记
  • 原文地址:https://www.cnblogs.com/learnordie/p/4657001.html
Copyright © 2011-2022 走看看