【华为OD-E卷-贪心的商人 100分(python、java、c++、js、c)】
题目
商人经营一家店铺,有 number 种商品,由于仓库限制每件商品的最大持有数量是 item[index],每种商品的价格是 item_price[item_index][day]。
通过对商品的买进和卖出获取利润,请给出商人在 days 天内能获取的最大的利润。
注:同一件商品可以反复买进和卖出
输入描述
- 输入描述 3 // 输入商品的数量 number
3 // 输入商人售货天数 days
4 5 6 // 输入仓库限制每件商品的最大持有数量是 item[index]
1 2 3 // 输入第一件商品每天的价格
4 3 2 // 输入第二件商品每天的价格
1 5 3 // 输入第三件商品每天的价格
输出描述
- 32 // 输出商人在这段时间内的最大利润
备注
- 根据输入的信息:
number = 3
days = 3
item[3] = {4, 5, 6}
item_price[3][4] = {{1, 2, 3}, {4, 3, 2}, {1, 5, 3}}
针对第一件商品,商人在第一天的价格是 item_price[0][0] = 1 时买入 item[0] 件,在第三天 item_price[0][2] = 3 的时候卖出,获利最大是 8; 针对第二件商品,不进行交易,获利最大时 0; 针对第三件商品,商人在第一天价格是 item_price[2][0] = 1 时买入 item[2] 件,在第二天 item_price[2][0] = 5 的时候卖出,获利最大是24; 因此这段时间商人能获取的最大利润是 8 + 24 = 32;
用例
用例一:
输入:
3
3
4 5 6
1 2 3
4 3 2
1 5 2
- 1
- 2
- 3
- 4
- 5
- 6
输出:
32
- 1
用例二:
输入:
1
1
1
1
- 1
- 2
- 3
- 4
输出:
0
- 1
python解法
- 解题思路:
- 这段代码模拟了一种多商品的交易收益计算。对于每种商品,判断在某些连续天数的价格上涨区间内可以产生的收益,并将所有商品的收益累加。
主要思路如下:
对于每种商品,遍历其价格数组,检查每一天的价格是否高于前一天。
如果当天价格高于前一天,则计算当日的收益(价格差
∗
∗ 当前商品的数量),并将其累加。
最终,将每种商品的总收益相加,得到整体收益。
# 输入商品种类数
number = int(input())
# 输入天数
days = int(input())
# 输入每种商品的数量列表
item = list(map(int, input().split()))
# 输入每种商品每天的价格矩阵
prices = [list(map(int, input().split())) for i in range(number)]
def getResult(number, days, item, prices):
ans = 0 # 总收益
for i in range(number): # 遍历每种商品
total_profit = 0 # 当前商品的收益
# 倒序遍历天数,计算相邻天数的价格差
for j in range(days - 1, 0, -1):
if prices[i][j] > prices[i][j - 1]: # 如果价格上涨
# 计算收益(价格差 * 商品数量)
total_profit += (prices[i][j] - prices[i][j - 1]) * item[i]
ans += total_profit # 累加该商品的收益到总收益
return ans
# 输出最终的收益
print(getResult(number, days, item, prices))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
java解法
- 解题思路
- 这段代码的目的是计算多种商品在多天的交易中,通过判断价格涨幅来获得的最大收益。对于每种商品,如果当天的价格高于前一天的价格,则可以通过出售商品获得收益,收益等于价格差乘以商品数量。最终,将所有商品的收益累加,得到总收益。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入商品数量和天数
int itemNum = sc.nextInt(); // 商品种类数
int dayCount = sc.nextInt(); // 天数
// 输入每种商品的限制数量
int[] limits = new int[itemNum];
for (int i = 0; i < itemNum; i++) {
limits[i] = sc.nextInt();
}
// 输入每种商品每天的价格矩阵
int[][] priceMatrix = new int[itemNum][dayCount];
for (int i = 0; i < itemNum; i++) {
for (int j = 0; j < dayCount; j++) {
priceMatrix[i][j] = sc.nextInt();
}
}
// 计算并输出最大收益
System.out.println(getMaxProfit(itemNum, dayCount, limits, priceMatrix));
}
/**
* 计算所有商品的最大收益
*
* @param itemNum 商品种类数
* @param dayCount 天数
* @param limits 每种商品的限制数量
* @param priceMatrix 商品的价格矩阵
* @return 最大收益
*/
public static int getMaxProfit(int itemNum, int dayCount, int[] limits, int[][] priceMatrix) {
int profit = 0; // 总收益
// 遍历每种商品
for (int i = 0; i < itemNum; i++) {
// 遍历商品每天的价格
for (int j = 1; j < dayCount; j++) {
// 如果当天价格高于前一天,计算收益
if (priceMatrix[i][j] > priceMatrix[i][j - 1]) {
profit += (priceMatrix[i][j] - priceMatrix[i][j - 1]) * limits[i];
}
}
}
return profit; // 返回总收益
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
C++解法
- 解题思路
更新中
- 1
C解法
更新中
- 1
JS解法
具体步骤如下:
输入解析:
第 1 行输入商品种类数。
第 2 行输入交易天数。
第 3 行输入每种商品的最大持有量(maxHoldings)。
接下来的输入是每种商品每天的价格表。
计算收益:
遍历每种商品的价格表。
如果某天的价格低于次日价格,则计算价格差乘以商品数量,作为该天的收益。
累加所有商品的收益,得到总收益。
// 使用 readline 模块处理多行输入
const rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const records = []; // 存储输入数据
let productCount, tradeDays, maxHoldings; // 定义商品数量、交易天数和每种商品的最大持有量
rl.on("line", (line) => {
records.push(line); // 读取每行输入
// 解析输入的前 3 行
if (records.length === 3) {
productCount = parseInt(records[0], 10); // 商品种类数
tradeDays = parseInt(records[1], 10); // 交易天数
maxHoldings = records[2].split(" ").map(Number); // 每种商品的最大持有量数组
}
// 当所有输入数据读取完成
if (productCount && records.length === productCount + 3) {
// 解析价格表数据
const priceChanges = records.slice(3).map((entry) =>
entry.split(" ").map(Number)
);
// 计算最大收益
console.log(
maximizeProfit(productCount, tradeDays, maxHoldings, priceChanges)
);
rl.close(); // 关闭输入流
}
});
/**
* 计算最大收益
* @param {number} products 商品种类数
* @param {number} days 交易天数
* @param {number[]} holdings 每种商品的最大持有量
* @param {number[][]} priceTable 每种商品每天的价格表
* @returns {number} 最大收益
*/
function maximizeProfit(products, days, holdings, priceTable) {
let profitSum = 0; // 总收益
// 遍历每种商品
for (let idx = 0; idx < products; idx++) {
// 遍历每天的价格
for (let day = 0; day < days - 1; day++) {
// 如果第二天的价格高于当天,计算收益
if (priceTable[idx][day] < priceTable[idx][day + 1]) {
profitSum +=
(priceTable[idx][day + 1] - priceTable[idx][day]) * holdings[idx];
}
}
}
return profitSum; // 返回总收益
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
注意:
如果发现代码有用例覆盖不到的情况,欢迎反馈!会在第一时间修正,更新。
解题不易,如对您有帮助,欢迎点赞/收藏
评论记录:
回复评论: