虚拟游戏理财

 在一款虚拟游戏中生活,你必须进行投资以增强在虚拟游戏中的资产以免被淘汰出局。现有一家 Bank,它提供有若干理财产品 m,风险及投资回报不同,你有 N(元)进行投资,能接受的总风险值为 X。
 你要在可接受范围内选择最优的投资方式获得最大回报。
  说明:
   1、在虚拟游戏中,每项投资风险值相加为总风险值;
   2、在虚拟游戏中,最多只能投资 2 个理财产品;
   3、在虚拟游戏中,最小单位为整数,不能拆分为小数; 投资额*回报率=投资回报
 输入描述:
  第一行:产品数(取值范围[1, 20]),总投资额(整数,取值范围[1, 10000]),可接受的总风险(整数,取值范围[1, 200])
  第二行:产品投资回报率序列,输入为整数,取值范围[1,60]
  第三行:产品风险值序列,输入为整数,取值范围[1,100]
  第四行:最大投资额度序列,输入为整数,取值范围[1,10000]
 输出描述:每个产品的投资额序列

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // 理财产品数量 m
        int m = sc.nextInt();
        // 总投资额 N
        int n = sc.nextInt();
        // 最大风险值 X
        int x = sc.nextInt();
        
        // 产品投资回报率序列
        int[] getList = new int[m];
        for(int i = 0; i < m; i++) {
            getList[i] = sc.nextInt();
        }
        
        // 产品风险值序列
        int[] xList = new int[m];
        for(int i = 0; i < m; i++) {
            xList[i] = sc.nextInt();
        }
        
        // 最大投资额度序列
        int[] nList = new int[m];
        for(int i = 0; i < m; i++) {
            nList[i] = sc.nextInt();
        }
        
        int maxGet = 0;
        int[][] bestPairs = new int[2][2];
        
        // 检查所有两两组合
        for(int i = 0; i < m; i++) {
            for(int j = i + 1; j < m; j++) {
                if(xList[i] + xList[j] > x) {
                    continue;
                }
                
                int iAmount, jAmount;
                if(nList[i] + nList[j] <= n) {
                    iAmount = nList[i];
                    jAmount = nList[j];
                } else {
                    if(getList[i] >= getList[j]) {
                        iAmount = Math.min(nList[i], n);
                        jAmount = n - iAmount;
                    } else {
                        jAmount = Math.min(nList[j], n);
                        iAmount = n - jAmount;
                    }
                }
                
                int currentGet = iAmount * getList[i] + jAmount * getList[j];
                
                if(currentGet > maxGet) {
                    maxGet = currentGet;
                    bestPairs[0][0] = i;
                    bestPairs[0][1] = iAmount;
                    bestPairs[1][0] = j;
                    bestPairs[1][1] = jAmount;
                }
            }
        }
        
        // 检查单个产品
        for(int i = 0; i < m; i++) {
            if(xList[i] > x) {
                continue;
            }
            
            int iAmount = Math.min(nList[i], n);
            int currentGet = iAmount * getList[i];
            
            if(currentGet > maxGet) {
                maxGet = currentGet;
                bestPairs[0][0] = -1;
                bestPairs[0][1] = 0;
                bestPairs[1][0] = i;
                bestPairs[1][1] = iAmount;
            }
        }
        
        int[] result = new int[m];
        if(bestPairs[0][0] != -1) {
            result[bestPairs[0][0]] = bestPairs[0][1];
        }
        result[bestPairs[1][0]] = bestPairs[1][1];
        
        for(int i = 0; i < m; i++) {
            System.out.print(result[i] + " ");
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容