首页 > 编程语言 >虚拟游戏理财 算法题

虚拟游戏理财 算法题

时间:2024-03-28 16:25:24浏览次数:16  
标签:游戏 int ++ nextInt 算法 虚拟 result new scanner

华为OD C卷 虚拟游戏理财

暴力枚举法。更好的方法是动态规划。菜鸡不会。

自己也写了,但是不全对,gpt写的,但是也有问题。自己修改后正确:

import java.util.*;

    public class InvestmentGame {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int m = scanner.nextInt(); // 产品数
            int totalInvestment = scanner.nextInt(); // 总投资额
            int totalRisk = scanner.nextInt(); // 可接受的总风险
            int[] returns = new int[m]; // 产品投资回报率序列
            int[] risks = new int[m]; // 产品风险值序列
            int[] maxInvestments = new int[m]; // 最大投资额度序列

            for (int i = 0; i < m; i++) {
                returns[i] = scanner.nextInt();
            }
            for (int i = 0; i < m; i++) {
                risks[i] = scanner.nextInt();
            }
            for (int i = 0; i < m; i++) {
                maxInvestments[i] = scanner.nextInt();
            }

            List<Integer> result = optimizeInvestment(m, totalInvestment, totalRisk, returns, risks, maxInvestments);
            for (Integer investment : result) {
                System.out.print(investment + " ");
            }
        }

        public static List<Integer> optimizeInvestment(int m, int totalInvestment, int totalRisk, int[] returns, int[] risks, int[] maxInvestments) {
            List<Integer> result = new ArrayList<>();
            int maxReturn = 0;
            int[] bestInvestments = new int[m];
            for (int i = 0; i < m; i++) {
                for (int j = i + 1; j < m; j++) {
                    for (int invest1 = 0; invest1 <= maxInvestments[i]; invest1++) {
                        for (int invest2 = 0; invest2 <= maxInvestments[j]; invest2++) {
                            if (risks[i] +  risks[j] <= totalRisk && invest1 + invest2 <= totalInvestment) {
                                int totalReturn = invest1 * returns[i] + invest2 * returns[j];
                                if (totalReturn > maxReturn) {
                                    bestInvestments = new int[m];
                                    maxReturn = totalReturn;
                                    bestInvestments[i] = invest1;
                                    bestInvestments[j] = invest2;
                                }
                            }
                        }
                    }
                }
            }
            for (int investment : bestInvestments) {
                result.add(investment);
            }
            return result;
        }
    }

输入为:

5 100 10
10 20 30 40 50
3 4 5 6 10
20 30 20 40 30

输出为:
0 30 0 40 0

标签:游戏,int,++,nextInt,算法,虚拟,result,new,scanner
From: https://www.cnblogs.com/jin-wen-xin/p/18101973

相关文章

  • linux虚拟机没有ip,网卡服务无法启动的解决
    最近使用虚拟机做实验,挂起虚拟机后再回复,发现经常无法使用xshell连接。进入虚拟机后,使用ifconfig命令查看网卡状态,发现网卡的ip没有了[root@host103~]#ifconfigens33      重启网卡,发现报错。查看网卡目录,也就只有这一个网卡文件,也就是不存在其他网卡配置错......
  • 知识图谱推理算法综述(下):基于语义的匹配模型
    背景知识图谱系统的建设需要工程和算法的紧密配合,在工程层面,去年蚂蚁集团联合OpenKG开放知识图谱社区,共同发布了工业级知识图谱语义标准OpenSPG并开源;算法层面,蚂蚁从知识融合,知识推理,图谱应用等维度不断推进。OpenSPGGitHub,欢迎大家Star关注~:https://github.com/Ope......
  • 【算法】归并排序(递归法)
    目录归并排序简介算法步骤(递归)C语言实现归并排序简介归并排序(mergesort)是一种高效、通用且基于比较的排序算法。由约翰·冯·诺依曼(JohnvonNeumann)于1945年发明,是一种分治算法(divide-and-conqueralgorithm)。时间复杂度为O(nlog⁡n)O{\left(n\log......
  • 08天【代码随想录算法训练营34期】第四章 字符串part02(KMP)
    KMP算法解决字符串匹配问题文本串aabaabaaf模式串aabaaf问:模式串是否在文本串中出现过?1)暴力解法,ptr指向文本串index0,遍历一遍发现不匹配,ptr再移向index1,遍历……依次重复,直到ptr指向32)KMP算法,ptr指向文本串index0,遍历到f发现不匹配,由于“aa”在字符串中index3和4时也出现......
  • 数据结构与算法题目集(中文)6-1 单链表逆转 C语言
    6-1单链表逆转本题要求实现一个函数,将给定的单链表逆转。函数接口定义:ListReverse(ListL);其中List结构定义如下:typedefstructNode*PtrToNode;structNode{ElementTypeData;/*存储结点数据*/PtrToNodeNext;/*指向下一个结点的指针*/};t......
  • 最近公共祖先(lca)倍增算法【模板】
    P3379【模板】最近公共祖先(LCA)-洛谷|计算机科学教育新生态(luogu.com.cn)#include<bits/stdc++.h>#include<cstdio>usingnamespacestd;constintN=5e5+100;constintinf=0x3f3f3f;intn,m,s;vector<int>g[N];intdep[N];//存u点的深度intfa[N][20];//存从u......
  • 代码随想录算法训练营第六十天|● 84.柱状图中最大的矩形
    柱状图中最大的矩形题目链接:84.柱状图中最大的矩形-力扣(LeetCode)思路:掌握了……吗?还是参考了下官网思路。代码随想录(programmercarl.com)classSolution{public:intlargestRectangleArea(vector<int>&heights){intresult=0;stack<int>st;......
  • FLASK学习记录-PIPENV虚拟环境搭建
     $pipinstallflask-ihttps://pypi.tuna.tsinghua.edu.cn/simpleLookinginindexes:https://pypi.tuna.tsinghua.edu.cn/simpleCollectingflaskDownloadinghttps://pypi.tuna.tsinghua.edu.cn/packages/93/a6/aa98bfe0eb9b8b15d36cdfd03c8ca86a03968a87f27ce22......
  • COMP2045Goofspiel游戏的程序实现
    COMP2045课程2024介绍这门课相当于模块分数的40%。它要求你写C++程序和报告,以解决下面描述的任务。这个的最后期限演习时间为2024年4月29日星期一晚上23:55。在开始练习之前,请阅读整个文档。如果你对这个练习有任何问题,请在问答论坛上提问Moodle,在讲座后,在实验室,或在广告中的办......
  • 算法小笔记0328
    1ios::sync_with_stdio(0);ios::sync_with_stdio(false);是C++中用于关闭C++输入输出流(iostream)与C输入输出库(stdio)同步的语句。默认情况下,C++的流库与C的stdio库是同步的,这意味着你可以混用cin,cout和scanf,printf等而不会出现问题。但是这种同步会导致性能下......