HihoCoder1037數字三角形(dp題)

oop 運動動態規劃思想
#include<iostream>
#include <string.h>
using namespace std;



const int m = 350;
class Angel
{
public:
    Angel();
    int intput();
    int solve();
    const int getResult() const;
private:
    int n;
    int a[m][m];
    int sum;
    
};

Angel::Angel()
{
    int sum =0;
    this->n = 0;
    this->a[m][m] = {0};
}
int Angel::intput()
{

    cin>>this->n;
    
    for(int i =0;i < n; i++)
    {
        for (int j = 0;j<= i; j++)
            
            cin>>this->a[i][j];
    }

    return 0;
}
int Angel::solve()
{
    for(int i =1;i < n; i++)
    {
        for (int j = 0;j<= i; j++)
        {
            if (j == 0)
            {
                a[i][j]=a[i-1][j]+a[i][j];
            }
            else if (j == i)
            {
                a[i][j]=a[i-1][j-1]+a[i][j];
            }
            else
            {
                a[i][j] =max(a[i-1][j],a[i-1][j-1])+a[i][j];
            }
        }
    }
    
    for (int i = 0; i <n ; i++)
    {
        if (a[n-1][i]>sum)
        {
            sum = a[n-1][i];
        }
    }

    return 0;
}

const int Angel::getResult() const
{
    return this->sum;
}
template <typename  T>

T max (T a,T b)
{
    return a>b? a:b;
}

int main(int argc , const char * argv[])
{
    
    Angel * a = new Angel();
    a->intput();
    a->solve();
    cout<<a->getResult();
    
   }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值