商品捆绑销售题目

来源:互联网 发布:天猫数据分析软件 编辑:程序博客网 时间:2024/04/29 00:46

同学发来的题目,让帮忙解决。
题目如下:
题目

源程序:
代码有可以优化的地方,也没有添加输入数据异常的判断,只是一个思路的实现(大神勿喷)。

#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;}
0 0
原创粉丝点击