You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
864 B

#include <bits/stdc++.h>
using namespace std;
int a[101][101] = {0};
int main() {
//输入+输出重定向
freopen("../DiTui/ShuZiSanJiaoXing.txt", "r", stdin);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; ++j) {
cin >> a[i][j];
}
}
//输出
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; ++j) {
cout << a[i][j] << " ";
}
cout << endl;
}
//从倒数第二层开始进行递推,一直推到第一层
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; ++j) {
a[i][j] = max(a[i + 1][j] + a[i][j], a[i + 1][j + 1] + a[i][j]);
}
}
cout << "数字三角形最大值:" << a[1][1] << endl;
//关闭文件
fclose(stdin);
return 0;
}