C++实现
时间: 2023-07-11 09:12:47 浏览: 72
C++语言实现如下:
```c++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
const int INF = 0x3f3f3f3f;
int val[N][N][N], dp[N][N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i % n + 1) {
val[i][i][j] = i * j * (j % n + 1);
}
for (int k = i + 1; k <= n + i - 1; k++) {
int p = k % n + 1;
val[i][p - 1][p] = i * p * (p % n + 1);
}
}
}
for (int len = 2; len <= n; len++) {
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = -INF;
for (int k = i; k < j; k++) {
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j] + val[i][k % n + 1][j % n + 1]);
}
}
}
cout << dp[1][n] << endl;
return 0;
}
```
和C语言实现的思路一样,只是语法上有所不同。
阅读全文