数字三角形问题
来源:互联网 发布:kali linux树莓派 编辑:程序博客网 时间:2024/06/03 22:06
#include "iostream"#include "algorithm"#include "fstream"using namespace std;/*注意:路径上的每一步只能从一个数走到下一层上和它最近的左边的数或者右边的数。d[i][j] 表示到达第i行,第j列时,最优路径上数字总和最大值d[i][j] = max{d[i-1][j], d[i-1][j-1]} + a[i][j] d[1][1] = a[1][1];*/int d[50][50];int a[50][50];int max(int a, int b){ return a>b? a:b;}int triangle(int n){ d[1][1] = a[1][1]; for(int i=2; i<=n; i++) for(int j=1; j<=i; j++) d[i][j] = max(d[i-1][j], d[i-1][j-1]) + a[i][j]; int max = d[n][1]; for(i=2; i<=n; i++) if(max < d[n][i]) max = d[n][i]; return max;}int main(){ ifstream fin("triangle.txt"); int n; cout << "三角形行数:"; fin >> n; cout << n; cout << "\n输入三角形:\n"; for(int i=1; i<=n; i++) { for(int j=1; j<=i; j++) { fin >> a[i][j]; cout << a[i][j] << " "; } cout << endl; } cout << "从顶到底经过的数字总和最大为:" << triangle(n) << endl; fin.close(); return 0;}
0 0
- 数字三角形问题
- 【dp】数字三角形问题
- 数字三角形问题求解
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- DP_数字三角形问题
- 【算法】数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- 数字三角形问题
- SDUToj1730数字三角形问题
- 运行Java命令行报错 class not found
- Android 自己画View -- drawable.draw 以及 drawText
- Plus One【leetcode】【89】【E】
- 天声人語 20151118 辺野古、「法廷闘争」へ
- 强大的STL之set
- 数字三角形问题
- 两个栈实现一个队列
- 日经春秋 20151118
- spring mvc 传中文 到controller层乱码的 解决方法
- LeetCode20:Valid Parentheses
- InputStreamReader FileReader
- 【Treap】[CQBZOJ2803]普通平衡树
- CSS:关于“-webkit-appearance: none”样式使用问题
- UITabBarController