葡萄酒酒进销存单机版

Problem statement:

问题陈述:

Given n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in the row. Let the initial profits from the wines be P1, P2, P3…Pn. On the Yth year, the profit from the ith wine will be Y*P[i], calculate the maximum profit from all the wines.

连续给出n种葡萄酒,其中整数分别表示每种葡萄酒的价格。 每年您都可以出售该行中的第一个或最后一个葡萄酒。 假设从葡萄酒中获得的初始利润为P1,P2,P3 ... Pn 。 在五年的Y,从第i个酒的利润将Y * P [I], 从所有的葡萄酒计算最大的利润

Input:

输入:

The first line of the input is T denoting the number of test cases. Then T test cases follow. Each test case contains two lines. The first line of each test case is a number N denoting the size of the price array of wine, the next line is N separated values of P[].

输入的第一行是T,表示测试用例的数量。 然后是T测试用例。 每个测试用例包含两行。 每个测试用例的第一行是一个数字N,表示葡萄酒价格数组的大小,下一行是N个独立的P []值。

Output:

输出:

For each test case output in a new line the max profit from the sale of all the wines.

对于新行中的每个测试用例输出,所有葡萄酒的销售都会获得最大利润。

Example with explanation:

带有说明的示例:

    Input:
T = 1 // Test case
N = 4
P = [1,4,2,3]
Output:
29
The optimal solution would be to sell the wines
in the order p1, p4, p3, p2
for a total profit 1 * 1 + 3 * 2 + 2 * 3 + 4 * 4 = 29.
Input:
T = 1 // Test case
N = 5
P = [2,3,5,1,4]
Output:
50
The optimal solution would be to sell the wines
in the order p1, p5, p4, p2, p3
for a total profit 2 * 1 + 4 * 2 + 1 * 3 + 3 * 4 + 5 * 5 = 50.

Solution Approach

解决方法

1) Recursive Approach

1)递归方法

Here we will try all possible solution(using all subproblems answer) then check the solution which gives the maximum answer. We will pick either the first wine and multiply with the current year and recursively move to next year or we will select the last wine and multiply with the current year and move recursively to the next part then we will select the maximum of the two subproblems for the current solution.

在这里,我们将尝试所有可能的解决方案(使用所有子问题答案),然后检查给出最大答案的解决方案。 我们将选择第一个葡萄酒并与当前年份相乘,然后递归移动到下一年;或者我们选择最后一个葡萄酒并与当前年份相乘,然后递归移动到下一部分,然后我们将选择两个子问题中的最大值当前的解决方案。

Pseudo Code:

伪代码:

int maxprofit(start, end, year)
{if (start > end)
return 0;
if (start <= end)
return max(P[start] * year + maxprofit(st + 1, end, year + 1), P[end] * year + maxprofit(st, end - 1, year + 1));
else
return 0;
}

Time Complexity:

时间复杂度:

The time complexity for the above approach is O(2^n), exponential time since we either take endpoint in the solution or we do not take the endpoint,that is we have two options for all the cases.

上面方法的时间复杂度是O(2 ^ n) ,因为我们要么在解决方案中采用端点,要么不采用端点,这是指数时间,即在所有情况下我们都有两个选择。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int maxprofit(int P[], int start, int end, int n, int year)
{if (start > end)
return 0;
else if (start <= end) {return max(P[start] * year + maxprofit(P, start + 1, end, n, year + 1), P[end] * year + maxprofit(P, start, end - 1, n, year + 1));
}
else
return 0;
}
int main()
{int t;
cout << "Enter number of test cases: ";
cin >> t;
while (t--) {int n;
cout << "Enter number of wines: ";
cin >> n;
int P[n];
cout << "Enter the wines prices: ";
for (int i = 0; i < n; i++)
cin >> P[i];
int start = 0;
int end = n - 1;
int year = 1;
int res = maxprofit(P, start, end, n, year);
cout << "Max Profit: ";
cout << res << "\n";
}
return 0;
}

Output

输出量

Enter number of test cases: 2
Enter number of wines: 4
Enter the wines prices: 1 4 2 3
Max Profit: 29
Enter number of wines: 5
Enter the wines prices: 2 3 5 1 4
Max Profit: 50

2) Dynamic Programming (Better Approach):

2)动态编程(更好的方法):

By carefully observing the recursion tree, we can see that we encounter the property of subproblem overlapping which can be prevented using memoization or dynamic programming.

通过仔细观察递归树,我们可以看到我们遇到了子问题重叠的属性,可以使用备注或动态编程来避免这种重叠。

We will use the 2-D array to store the profit for a particular year, initially, all the profit from the sale is zero. For memoization, we will use the start and end state. If the dp[start][end] is equal to zero it means we haven't solved for that year and if the dp[start][end] is not equal to zero then that dp[start][end] is returned.

我们将使用二维数组存储特定年份的利润,最初,所有销售利润均为零。 为了便于记忆,我们将使用开始和结束状态。 如果dp [start] [end]等于零,则表示那一年我们还没有解决,如果dp [start] [end]不等于零,则返回dp [start] [end]。

Pseudo Code:

伪代码:

int maxprofit(start, end, year)
{if (start > end)
return 0;
if (dp[start][end] != 0)
return dp[start][end];
else if (start <= end)
return dp[start][end] = max(P[start] * ye + maxprofit(start + 1, end, year + 1), P[end] * year + maxprofit(start, end - 1, year + 1));
else
return 0;
}

Time Complexity:

时间复杂度:

The time complexity for the above case is O(N^2), where N is the number of wines.

上述情况的时间复杂度为O(N ^ 2) ,其中N是葡萄酒的数量。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int dp[1004][1004];
int maxprofit(int P[], int start, int end, int n, int year)
{if (start > end)
return 0;
else if (dp[start][end] != 0)
return dp[start][end];
else {dp[start][end] = max(P[start] * year + maxprofit(P, start + 1, end, n, year + 1), P[end] * year + maxprofit(P, start, end - 1, n, year + 1));
return dp[start][end];
}
}
int main()
{int t;
cout << "Enter number of test cases: ";
cin >> t;
while (t--) {int n;
cout << "Enter number of wines: ";
cin >> n;
int P[n];
cout << "Enter the wines prices: ";
for (int i = 0; i < n; i++)
cin >> P[i];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = 0;
int start = 0;
int end = n - 1;
int year = 1;
int res = maxprofit(P, start, end, n, year);
cout << "Max Profit: ";
cout << res << "\n";
}
return 0;
}

Output

输出量

Enter number of test cases: 2
Enter number of wines: 6
Enter the wines prices: 2 5 6 2 4 5
Max Profit: 93
Enter number of wines: 5
Enter the wines prices: 2 3 5 1 4
Max Profit: 50

翻译自: https://www.includehelp.com/icp/wine-selling-problem.aspx

葡萄酒酒进销存单机版

葡萄酒酒进销存单机版_葡萄酒销售问题| 找到销售葡萄酒的最大利润相关推荐

  1. 药品进销存管理系统_药一点软件_连锁版介绍符合GSP最新要求

    药品进销存管理系统_药一点软件_连锁版介绍符合GSP最新要求 ● 各证照到期预警及自动过期停用,首营企业.品种及不合格药品管理等等...... ● 全国多个地区药监部门推荐本产品并顺利通过GSP检验 ...

  2. 企业进销存管理系统_攻略 | 七巧Plus定制您的专属进销存管理系统

    什么是进销存? 进销存是指企业管理中,对采购.入库.销售的动态管理过程.企业通过进销存系统,可以随时掌握从采购到销售一系列的信息流.资金流.乃至物流信息,以便做出此时商业活动的最佳决策,获得最大效益. ...

  3. excel进销存管理系统_通用Excel助力企业定制开发信息化系统常用功能模块

    信息化成为现代企业管理的趋势,而企业管理系统则是推动这一趋势发展的重要载体.那么什么是企业管理系统呢?企业管理系统都有那些呢? 通用Excel助力企业定制开发信息化系统常用功能模块 企业管理系统,是指 ...

  4. excel进销存管理系统_【实例分享】勤哲Excel服务器做企业进销存财务管理系统...

    如今,企业在推进数字化选型的时常有很多焦虑,难以找到合适的产品正是其中之一.正如一家正在挑选信息化管理软件的企业,对信息化产品的理解与厂商提供的产品可能完全不同,企业真正想要的是具备发货管理之类功能完 ...

  5. excel进销存管理系统_美萍商业进销存软件—库存了如指掌

    进销存软件是从商品的采购(进)到入库(存)到销售(销)进行管理的软件,随着信息技术的飞速发展,企业进销存的管理应用相应的软件使这一动态的进销存过程更加有条理,应用进销存管理软件,不仅使企业的进销存管理 ...

  6. 进销存系统_系统介绍核心模块划分功能流程介绍(1)

    一.学习目标 二.进销存系统简介 进销存系统是为了对企业生产经营中进货.出货.批发销售.付款等全程进行(从接获订单合同开始.进入物料采购.入库.领用到产品完工入库.交货.回收货款.支付原材料款等)跟踪 ...

  7. 进销存系统_用户信息更新密码修改(3)

    一.完善登录功能 1.1.注释掉验证码相关代码 修改index.js,先注释掉验证码相关代码 修改main.tfl 二.用户信息更新&密码修改 2.1.存放静态资源 2.2.编写后台代码 Ma ...

  8. java 进销存系统_基于SSM框架搭建的java web商家进销存网站系统

    项目描述 根据当前的服装库存管理体制,一般系统,总是根据所掌握的商品类别,相应分成几个库存管理员进行商品的计划.订货.核对入库:根据销售的需要来发送物品(出库).并定期进行库存盘点.作台账:根据服装管 ...

  9. 进销存系统_项目环境搭建代码生成登录接口(2)

    一.环境搭建 1.1.项目使用Maven 多模块进行构建 1.2.添加静态资源 二.用户模块设计 2.1.E-R图 2.1.SQL脚本 /*Navicat Premium Data TransferS ...

  10. 进销存系统_用户角色分配(12)

    一.用户角色分配 1.1.用户角色分配界面原型 添加用户记录 更新用户记录 1.2.用户角色分配实现流程 添加用户-角色分配 更新用户-角色分配 代码 RoleServiceImpl.java @Ov ...

最新文章

  1. 均匀分布取某一点概率_概率和概率分布
  2. JQuery中$.ajax()分享
  3. java jframe显示图片_java怎么在JFrame中显示动态图片
  4. keil obj 文件 结构_【Python】数据分析前的入门教程 Python For Everybody P2:数据结构...
  5. https协议 ppt 下载卷_做PPT被版权吓得心颤颤?教你如何搞到靠谱素材
  6. 工业以太网在工业控制中的运用
  7. python getattr函数_Python中的getattr()函数详解
  8. Mysql数据库的读写分离
  9. 开启Golang编程第一章
  10. mysql 2049_mysql数据库备份与还原,解决40101和ERROR 2049错误
  11. 如何判断自己的Windows系统是否为盗版系统?
  12. 高斯过程回归(输出学习法!)
  13. 【分库分表ShardingSphere】
  14. v3.exo是什么文件_exo是什么文件?
  15. What is the difference of PO Charge Account,PO Accrual Account and PO Variance Account
  16. 【攻防世界-Web简单篇】
  17. 华为5g鸿蒙折叠,华为再次亮剑!5G新旗舰已经确认,折叠屏+升级到鸿蒙2.0,价格过万...
  18. 向左转移测试需要整个团队的努力
  19. java 连接teradata_java连接teradata | 学步园
  20. 谷歌支付获取refresh token

热门文章

  1. java插件化设计开发
  2. XILINX FPGA数字信号处理——3、数字的表示和运算的实现
  3. 可行性研究报告模板 Shane版
  4. 在几何画板中如何制作圆柱的侧面展开动画_几何画板制作圆柱展开图过程详解...
  5. (二)零基础入门C语言 --- C语言之入门课程
  6. 陈纪修 数学分析,上下册
  7. oracle 9i,10G,11G,各版本下载资源(使用迅雷),收集好久,分享上来!
  8. oracle grant的用法,oracle grant总结
  9. 服务器双硬盘系统安装系统安装,固态机械混合安装教程!双硬盘安装系统的方法...
  10. 计算机固态硬盘安装,电脑安装了固态硬盘,需要如何重装系统?详细的方法教程在这里!...