投资分配

 1 //最优策略应该是2,0,3,1,现在 只会求最大效益 
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 using namespace std;
 6 int ans[5];//保存最优策略 ,一维向量 
 7 int g[5][8] = {//投资0到6w时,各个工厂获得的效益 
 8                 {0,20,50,65,80,85,85},
 9                 {0,20,40,50,55,60,65},
10                 {0,25,60,85,100,110,115},
11                 {0,25,40,50,60,65,70}
12             };
13 int f[5][7];
14 int main()
15 {
16     int i,j,k,t,T;
17     memset(f,0,sizeof(f));
18     memset(ans,0,sizeof(ans));
19     t = 0;
20     int next ;
21     for(k=1;k<=4;k++)
22     {
23         int Max = 0;
24         for(i=0;i<=6;i++)
25         {
26             for(j=0;j<=i;j++)
27             {
28                 if(Max<(g[k-1][j]+f[k-1][i-j]))//因为g数组中行标从 0开始,所以g的下标也是k-1不是k 
29                 {
30                     next = j;
31                     Max = g[k-1][j]+f[k-1][i-j];
32                 }
33             }  
34             f[k][i] = Max;
35             ans[t++] = next;
36         }
37     }         
38     cout<<"最大利润"<<f[4][6]<<endl;
39     cout<<"最优策略 :";
40     for(i=0;i<3;i++)
41         cout<<ans[i]<<",";
42     cout<<ans[3]<<endl;
43     system("pause");
44 }
45         
46         
原文地址:https://www.cnblogs.com/hxsyl/p/2651236.html