/*---------------------------------------------------------------------------
File name:      C76-58.cpp
Author:         HJin
Created on:     7/2/2007 19:44:06
Environment:    Windows XP Professional SP2 English +
                Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
Problem statement:
---------------------------------------------------------------------------
经典 76 道编程题 之 58:
58. 将7万元投资到A,B,C三项目上,其利润见下表:
    投资额(万元)│  1     2    3      4     5     6     7
    ──────┼────────────────────
        项  A   │0.11  0.13  0.15  0.24  0.24  0.30  0.35
            B   │0.12  0.16  0.21  0.25  0.25  0.29  0.34
        目  C   │0.08  0.12  0.20  0.26  0.26  0.30  0.35
如何分配投资额,使获得的利润最大。
Analysis:
---------------------------------------------------------------------------
Just use a double loop to search maximum for A[i] + B[j] + C[k]
such that i+j+k = 7.
Sample output:
---------------------------------------------------------------------------
0.53 = 0.11 + 0.16 + 0.26.
Plan is to invest 1 万元 for 项目 A, 2 万元 for 项目 B, 4 万元 for 项目 C.
maximum profit is 0.53
Press any key to continue . . .
Reference:
1. 野比, [全民编程]76道高难度C++练习题
https://bbs.bc-cn.net/viewthread.php?tid=147967
*/
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
    int i;
    int j;
    int k;
    int best_i;
    int best_j;
    int best_k;
    float profit;
    float max_profit = 0.0;
    // data --- note that we added the first column for 0 investment
    float A[] = {0.0, 0.11,  0.13,  0.15,  0.24,  0.24,  0.30,  0.35};
    float B[] = {0.0, 0.12,  0.16,  0.21,  0.25,  0.25,  0.29,  0.34};
    float C[] = {0.0, 0.08,  0.12,  0.20,  0.26,  0.26,  0.30,  0.35};
    // double loop to search maximum for A[i] + B[j] + C[k]
    // such that i+j+k = 7.
    for(i=0; i<8; ++i)
    {
        for(j=0; j<=7-i; ++j)
        {
            profit = A[i];
profit += B[j];
            k = 7-i-j;
            if(k>=0)
            {
                profit += C[k];
            }
            if(max_profit < profit)
            {
                max_profit = profit;
                best_i = i;
                best_j = j;
                best_k = k;
            }
        }    
    }
    // outputs
    cout<<max_profit<<" = "
        <<A[best_i]<<" + "
        <<B[best_j]<<" + "
        <<C[best_k]<<".\n";
    cout<<"Plan is to invest " <<best_i<<" 万元 for 项目 A, "
        <<best_j<<" 万元 for 项目 B, "
        <<best_k<<" 万元 for 项目 C.\n";
    cout<<"maximum profit is "<<max_profit<<endl;
    return 0;
}

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.



 
											






 
	    

 
	



 程序代码:
程序代码:
