zoukankan      html  css  js  c++  java
  • 数塔 HDU 2084

    题目

    给定一个具有 N 层的数字三角形,从顶至底有多条路径,每一步可沿左斜线向下或沿右斜线向下,路径所经过的数字之和为路径得分,请求出最大路径得分。
    在这里插入图片描述

    输入

    1
    5
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5

    输出

    30

    代码

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int main ()
    {
    	int x,m,number[m+1][m+1];
    	memset(number,0,sizeof(number));
    	cin>>x;
    	while(x--)
    	{
    		cin>>m;
    		for(int i=1;i<=m;i++)
    	 	    for(int j=1;j<=i;j++)
    		 	    cin>>number[i][j];
    		for(int i=m-1;i>=1;i--)
    		    for(int j=1;j<=i;j++)
    			    number[i][j]+=max(number[i+1][j],number[i+1][j+1]);
    		cout<<number[1][1]<<endl;
    	}
    	return 0;
    }
    

    看着好像挺对的,但是,Runtime Error(ACCESS_VIOLATION)。

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int main ()
    {
    	int x,m,number[170][170];
    	cin>>x;
    	while(x--)
    	{
    		cin>>m;
    		memset(number,0,sizeof(number));
    		for(int i=1;i<=m;i++)
    	 	    for(int j=1;j<=i;j++)
    		 	    cin>>number[i][j];
    		for(int i=m-1;i>=1;i--)
    		    for(int j=1;j<=i;j++)
    			    number[i][j]+=max(number[i+1][j],number[i+1][j+1]);
    		cout<<number[1][1]<<endl;
    	}
    	return 0;
    }
    

    总结:能不开变长数组,就不开变长数组!

  • 相关阅读:
    Go
    list的基本操作实现
    天梯赛练习题L2-006. 树的遍历
    部署 Fluent Bit ( td-agent-bit )
    elastalert + supervisor
    elastalert搭建
    Docker 部署 kibana( ES开启了密码认证)
    Docker 部署 elasticsearch( ES开启了密码认证)
    Python yaml模块
    Python json和pickle模块
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338940.html
Copyright © 2011-2022 走看看