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


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

  • 相关阅读:
    Python实现客观赋权法
    Python实现熵值法确定权重
    正则化项L1和L2
    特征工程的归一化和标准化
    CentOS下Neo4j安装教程
    Window下Neo4j安装教程
    Window下JDK安装教程
    Git 命令
    Kubernetes 资源清单 常用字段,Pod 实例
    kubernetes 集群搭建
  • 原文地址:https://www.cnblogs.com/learnordie/p/4657001.html
Copyright © 2011-2022 走看看