给下面代码增添注释 #include <iostream> #include <cstring> #include <algorithm> using namespace std; int profit[4][7] = { {0, 0, 0, 0, 0, 0, 0}, {0, 3, 5, 6, 7, 6, 5}, {0, 4, 6, 7, 8, 9, 10}, {0, 2, 5, 9, 8, 8, 7}}; int dp[10][10][10]; int main() { memset(dp, 0, sizeof(dp)); for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) for (int k = 0; k < 7; k++) { dp[i][j][k] = max(dp[i][j][k], profit[1][i] + profit[2][j] + profit[3][k]); } int l = -1, r = -1, m = -1, res = -1; for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) for (int k = 0; k < 7; k++) if ((i + k + j <= 6 && i + k + j >= 4) && res < dp[i][j][k]) { res = dp[i][j][k]; l = i; r = j; m = k; } printf("总设备数:%d\n", l + r + m); printf("分厂1分配设备数:%d\n分厂2分配设备数:%d\n分厂3分配设备数:%d\n", l, r, m); printf("总利润为:%d", res); return 0; }
时间: 2024-01-28 19:04:53 浏览: 33
```
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int profit[4][7] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 3, 5, 6, 7, 6, 5},
{0, 4, 6, 7, 8, 9, 10},
{0, 2, 5, 9, 8, 8, 7}
};
// profit数组用来存储三个长度为7的数组A、B、C中的数字
int dp[10][10][10];
// dp数组用来存储所有可能的三元组之和
int main() {
// 初始化dp数组为0
memset(dp, 0, sizeof(dp));
// 遍历所有可能的三元组,并将它们的和存储到dp数组中
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
for (int k = 0; k < 7; k++) {
dp[i][j][k] = max(dp[i][j][k], profit[1][i] + profit[2][j] + profit[3][k]);
// 选出A数组中第i个数字,B数组中第j个数字,C数组中第k个数字,计算它们的和,并将其与dp[i][j][k]比较,取其中最大值
}
int l = -1, r = -1, m = -1, res = -1;
// l、r、m分别表示三个数组中选出的数字的下标,res表示选出三元组的和的最大值
// 遍历dp数组,找到所有满足下标之和在4到6之间的三元组,并记录其中和最大的三元组和对应的下标
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
for (int k = 0; k < 7; k++)
if ((i + k + j <= 6 && i + k + j >= 4) && res < dp[i][j][k]) {
res = dp[i][j][k];
l = i;
r = j;
m = k;
}
// 输出结果
printf("总设备数:%d\n", l + r + m);
printf("分厂1分配设备数:%d\n分厂2分配设备数:%d\n分厂3分配设备数:%d\n", l, r, m);
printf("总利润为:%d", res);
return 0;
}
```
注释中解释了代码的功能、变量的含义以及算法的实现思路。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)