/******************************************************************** * @file Main_practise.cpp * @date 2014-9-21 * @author Tiger * @brief 数字三角形 * @details 动态规划-递推计算 ********************************************************************/ #include <cstdio> #include <algorithm> const int NUM = 5; int Data[NUM][NUM] = { { 7 }, { 3, 8 }, { 8, 1, 0 }, { 2, 7, 4, 4 }, { 4, 5, 2, 6, 5 } }; int Sum[NUM][NUM]; int solve(int i, int j); int main(int argc, const char* argv[]) { for (int i=0; i<NUM; ++i) { for (int j=0; j<NUM; ++j) { Sum[i][j] = -1; } } printf("%d\n", solve(0, 0)); return 0; } int solve(int i, int j) { if (Sum[i][j] >= 0) { return Sum[i][j]; } else { return Sum[i][j] = Data[i][j] + (i == NUM-1 ? 0 : std::max(solve(i+1, j), solve(i+1, j+1))); } }
时间: 2024-10-08 07:41:21