商品捆綁銷售題目

同學發來的題目,讓幫忙解決。
題目如下:
題目

源程序:
代碼有可以優化的地方,也沒有添加輸入數據異常的判斷,只是一個思路的實現(大神勿噴)。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

#define N 10

//最大利潤
int main()
{
    int num;
    int comb_num;//組合
    int cnt;
    int i,j;
    int max;
    int temp_i;
    int temp_j;
    int total_profit = 0;
    int profit_list[N][N];
    //測試1
    /*int profit_list[N][N]= {
    {0,  6,  62, 13},
    {6,  0,  35, 94},
    {62, 35, 0,  5},
    {13, 94, 5,  0}
    };*/
    //測試2
    /*int profit_list[N][N]= {
        {0,   6,   62,  13, 1},
        {6,   0,   35,  94, 2},
        {62, 35,   0,   5,  3},
        {13, 94,   5,   0,  4},
        {1,   2,   3,   4,  0}
    };*/ 
    cout<<"輸入商品個數:";
    cin>>num;
    cout<<"輸入利潤列表:"<<endl;
    for ( i = 0; i < num; i++)
    {
        for ( j = 0; j < num; j++)
        { 
            cin>>profit_list[i][j];
        }
    }

    comb_num = num/2;
    cnt = 0;
    max = profit_list[0][0];

    while (cnt < comb_num)
    {
        for ( i = 0; i < num; i++)
        {
            for ( j = i; j < num; j++)
            {
                if (profit_list[i][j] > max)
                {
                    temp_i = i;
                    temp_j = j;
                    max = profit_list[i][j];//找到最大值
                }
            }
        }

        total_profit = total_profit + max;//記錄最大利潤

        for ( i = 0; i < num; i++)
        {
            for ( j = 0; j < num; j++)
            {
                if ((i == temp_i) || (j == temp_j) || (i == temp_j) || (j == temp_i))
                {
                    profit_list[i][j] = 0;//清零
                }
            }
        }

        cnt ++;
        max = 0;

    }

    cout<<"total_profit:"<<total_profit<<endl;


    cout<<endl;
    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章