一、题目描述
题目描述:
在一款虚拟游戏中生活,你必须进行投资以增强在虚拟游戏中的资产以免被淘汰出局。
现有一家Bank,它提供有若干理财产品 m 个,风险及投资回报不同,你有 N(元)进行投资,能接收的总风险值为X。
你要在可接受范围内选择最优的投资方式获得最大回报。备注:
在虚拟游戏中,每项投资风险值相加为总风险值;
在虚拟游戏中,最多只能投资2个理财产品;
在虚拟游戏中,最小单位为整数,不能拆分为小数;
投资额*回报率=投资回报
二、输入输出
输入描述:
第一行:
产品数(取值范围[1,20])
总投资额(整数,取值范围[1, 10000])
可接受的总风险(整数,取值范围[1,200])
第二行:产品投资回报率序列,输入为整数,取值范围[1,60]
第三行:产品风险值序列,输入为整数,取值范围[1, 100]
第四行:最大投资额度序列,输入为整数,取值范围[1, 10000]输出描述:
每个产品的投资额序列
三、测试用例
用例
输入:
5 100 10
10 20 30 40 50
3 4 5 6 10
20 30 20 40 30
输出:
0 30 0 40 0
说明:
投资第二项30个单位,第四项40个单位,总的投资风险为两项相加为4+6=10
四、考点内容
考察遍历、枚举问题
五、参考代码
JAVA代码
// import java.util.regex.Matcher;
// import java.util.stream.Collectors;
// import java.util.*;
// import java.util.HashMap;
// import java.util.regex.Pattern;
// import java.util.Scanner;
// import java.util.stream.Stream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取输入参数
int[] inputParams = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int productCount = inputParams[0]; // 产品数
int totalInvestment = inputParams[1]; // 总投资
int totalRisk = inputParams[2]; // 总风险
int[] backReturns = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] risks = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] investments = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int maxInvestBack = 0;
int maxInvestBackRisk = Integer.MAX_VALUE;
HashMap<Integer, Integer> selection = new HashMap<>();
// 选择最佳投资方案
for (int i = 0; i < productCount; i++) {
if (risks[i] <= totalRisk) {
int investI = Math.min(investments[i], totalInvestment);
int investBack = investI * backReturns[i];
if (investBack > maxInvestBack || (investBack == maxInvestBack && risks[i] < maxInvestBackRisk)) {
maxInvestBack = investBack;
maxInvestBackRisk = risks[i];
selection.clear();
selection.put(i, investI);
}
} else {
continue;
}
for (int j = i + 1; j < productCount; j++) {
if (risks[i] + risks[j] <= totalRisk) {
int investI;
int investJ;
if (backReturns[i] > backReturns[j]) {
investI = Math.min(totalInvestment, investments[i]);
investJ = Math.min(totalInvestment - investI, investments[j]);
} else if (backReturns[i] < backReturns[j]) {
investJ = Math.min(totalInvestment, investments[j]);
investI = Math.min(totalInvestment - investJ, investments[i]);
} else if (risks[i] > risks[j]) {
investJ = Math.min(totalInvestment, investments[j]);
investI = Math.min(totalInvestment - investJ, investments[i]);
} else {
investI = Math.min(totalInvestment, investments[i]);
investJ = Math.min(totalInvestment - investI, investments[j]);
}
int investBack = investI * backReturns[i] + investJ * backReturns[j];
int investBackRisk = risks[i] + risks[j];
if (investBack > maxInvestBack || (investBack == maxInvestBack && investBackRisk < maxInvestBackRisk)) {
maxInvestBack = investBack;
maxInvestBackRisk = investBackRisk;
selection.clear();
if (investI > 0) selection.put(i, investI);
if (investJ > 0) selection.put(j, investJ);
}
}
}
}
// 输出最终选择的投资方案
StringJoiner result = new StringJoiner(" ");
for (int i = 0; i < productCount; i++) {
if (selection.containsKey(i)) {
result.add(selection.get(i) + "");
} else {
result.add("0");
}
}
System.out.println(result);
}
}
Python代码
# from collections import Counter, defaultdict
# from queue import Queue
# import os
# import math
# import functools
# import re
# import copy
# import sys
import sys
def optimize_investment(number, N, X, return_list, risk_list, max_cost_list):
return_list.append(0)
risk_list.append(0)
max_cost_list.append(0)
number += 1
max_return = 0
max_status = ["0"] * number
max_return_risk = 0
for i in range(number):
for j in range(i + 1, number):
if risk_list[i] + risk_list[j] <= X:
max_return_product_index = i if return_list[i] > return_list[j] else j
other_return_product_index = i + j - max_return_product_index
max_return_cost = min(N, max_cost_list[max_return_product_index])
other_return_cost = min(N - max_return_cost, max_cost_list[other_return_product_index])
cur_return = return_list[max_return_product_index] * max_return_cost + return_list[
other_return_product_index] * other_return_cost
if cur_return > max_return:
max_return = cur_return
max_return_risk = risk_list[i] + risk_list[j]
max_dict = {max_return_product_index: max_return_cost, other_return_product_index: other_return_cost}
for k in range(number):
max_status[k] = str(max_dict.get(k, 0))
return " ".join(max_status[:-1])
# 读取输入
number, N, X = map(int, input().split())
return_list = list(map(int, input().split()))
risk_list = list(map(int, input().split()))
max_cost_list = list(map(int, input().split()))
# 调用函数并输出结果
max_status = optimize_investment(number, N, X, return_list, risk_list, max_cost_list)
print(max_status)
C++代码
// #include<utility>
// #include<algorithm>
// #include<unordered_map>
// #include<set>
// #include<map>
// #include<list>
// #include<stdlib.h>
// #include<string.h>
// #include<iostream>
// #include<regex>
// #include<cmath>
// #include<queue>
// #include<exception>
// #include<stack>
// #include<bitset>
#include <bits/stdc++.h>
using namespace std;
// 优化投资选择
void optimizeInvestment() {
int productCount, totalInvestment, totalRisk;
cin >> productCount >> totalInvestment >> totalRisk;
vector<int> backReturns(productCount);
vector<int> risks(productCount);
vector<int> investments(productCount);
// 读取背景收益、风险和投资
for (int i = 0; i < productCount; i++) {
cin >> backReturns[i];
}
for (int i = 0; i < productCount; i++) {
cin >> risks[i];
}
for (int i = 0; i < productCount; i++) {
cin >> investments[i];
}
int maxInvestBack = 0;
int maxInvestBackRisk = INT_MAX;
map<int, int> selection;
// 选择最佳投资方案
for (int i = 0; i < productCount; i++) {
if (risks[i] <= totalRisk) {
int investI = min(investments[i], totalInvestment);
int investBack = investI * backReturns[i];
if (investBack > maxInvestBack || (investBack == maxInvestBack && risks[i] < maxInvestBackRisk)) {
maxInvestBack = investBack;
maxInvestBackRisk = risks[i];
selection.clear();
selection[i] = investI;
}
} else {
continue;
}
for (int j = i + 1; j < productCount; j++) {
if (risks[i] + risks[j] <= totalRisk) {
int investI, investJ;
if (backReturns[i] > backReturns[j]) {
investI = min(totalInvestment, investments[i]);
investJ = min(totalInvestment - investI, investments[j]);
} else if (backReturns[i] < backReturns[j]) {
investJ = min(totalInvestment, investments[j]);
investI = min(totalInvestment - investJ, investments[i]);
} else if (risks[i] > risks[j]) {
investJ = min(totalInvestment, investments[j]);
investI = min(totalInvestment - investJ, investments[i]);
} else {
investI = min(totalInvestment, investments[i]);
investJ = min(totalInvestment - investI, investments[j]);
}
int investBack = investI * backReturns[i] + investJ * backReturns[j];
int investBackRisk = risks[i] + risks[j];
if (investBack > maxInvestBack || (investBack == maxInvestBack && investBackRisk < maxInvestBackRisk)) {
maxInvestBack = investBack;
maxInvestBackRisk = investBackRisk;
selection.clear();
if (investI > 0) selection[i] = investI;
if (investJ > 0) selection[j] = investJ;
}
}
}
}
// 输出最终选择的投资方案
for (int i = 0; i < productCount; i++) {
if (selection.count(i) > 0) {
cout << selection[i] << " ";
} else {
cout << "0 ";
}
}
cout << endl;
}
int main() {
optimizeInvestment();
return 0;
}