投资6万元,拟投产某工厂A、B、C三种产品,其利润如下表所示,利用所学的动态规划法求解资源分配问题的方法求最佳投资方案。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INVESTMENT = 60000;
const int NUM_PRODUCTS = 3;
const int NUM_STEPS = 7;
double profits[NUM_PRODUCTS][NUM_STEPS] = {
{0, 1.2, 1.5, 1.85, 2.4, 2.8, 3.3},
{0, 1.8, 2.0, 2.25, 2.4, 2.5, 2.6},
{0, 1.3, 1.9, 2.2, 2.45, 2.7, 3.0}
};
void dynamicProgramming() {
vector<vector<double>> dp(NUM_PRODUCTS + 1, vector<double>(INVESTMENT + 1, 0));
for (int i = 1; i <= NUM_PRODUCTS; i++) {
for (int j = 0; j <= NUM_STEPS; j++) {
for (int k = 0; k <= INVESTMENT; k++) {
if (j * 10000 <= k) {
dp[i][k] = max(dp[i][k], dp[i - 1][k - j * 10000] + profits[i - 1][j]);
}
}
}
}
cout << "最佳投资方案及利润:" << endl;
cout << "总投资额:" << INVESTMENT << endl;
for (int i = 1; i <= NUM_PRODUCTS; i++) {
cout << "产品 " << char('A' + i - 1) << ": " << dp[i][INVESTMENT] << endl;
}
}
int main() {
dynamicProgramming();
return 0;
}