腾讯暑期实习生机试题 - 打怪兽 java
发布日期:2021-06-20 05:37:16
浏览次数:17
分类:技术文章
本文共 2975 字,大约阅读时间需要 9 分钟。
本题测试样例
46 2 4 91 1 1 2输出:346 2 8 9 161 1 1 2 2输出:331 1 51 1 2输出:3
import java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * @program: 个人demo * @description: 每次都是pass * @author: Mr.Hu * @create: 2019-03-29 19:07 */public class OJ { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) {//注意while处理多个case int n = in.nextInt(); long wuli = 0, cost = 0; long[] array = new long[n]; Listlist1 = new ArrayList<>(); //花费为1的怪兽 List list2 = new ArrayList<>(); //花费为2的怪兽 wuli = in.nextLong(); for (int i = 1; i < n; i++) { array[i] = in.nextLong(); } cost = in.nextLong(); for (int i = 1; i < n; i++) { int nextCost = in.nextInt(); if (wuli >= array[i]) { //打得过记录下 if (nextCost == 1) list1.add(new monster(array[i], nextCost)); else list2.add(new monster(array[i], nextCost)); } else { if (list1.size()>0 &&((nextCost == 1 && list1.get(0).wu >= array[i]) || (nextCost == 2 && list1.get(0).wu + wuli >= array[i]))) { //买以前的怪兽1实惠 cost += 1; wuli += list1.get(0).wu; list1.remove(0); if (nextCost==1) list1.add(new monster(array[i], nextCost)); //不买这个 我记录下 else list2.add(new monster(array[i], nextCost)); } else if (list2.size()>0 && nextCost == 2 && list2.get(0).wu > array[i]) { //买以前的2怪兽实惠 cost += list2.get(0).money; wuli += list2.get(0).wu; list2.remove(0); list2.add(new monster(array[i], nextCost)); //不买这个 我记录下 }else if (list1.size()>1 && nextCost == 2 && list1.get(1).wu +list1.get(0).wu + wuli >= array[i]){ //买俩1 cost += 2; wuli += (list1.get(0).wu+list1.get(1).wu); list1.remove(0); list1.remove(0); if (nextCost==1) list1.add(new monster(array[i], nextCost)); //不买这个 我记录下 else list2.add(new monster(array[i], nextCost)); } else { //买这个怪兽实惠 cost += nextCost; wuli += array[i]; } } } System.out.println(cost); } } static class monster implements Comparable { long wu; float money; public monster(long wu, int money) { this.wu = wu; this.money = money; } @Override public int compareTo(monster o) { //按武力值排序 if (o.wu == this.wu) return 0; else return o.wu > this.wu ? 1 : -1; } }}
转载地址:https://blog.csdn.net/h2453532874/article/details/89054660 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月08日 13时55分49秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Android入门笔记18 字符串资源 国际化 数组资源
2019-05-08
Android入门笔记19 图片资源
2019-05-08
909422229_前端框架layUI
2019-05-08
909422229_系统登录之防sql注入Utils
2019-05-08
909422229_HTML事件冒泡,多个事件同时触发问题
2019-05-08
909422229_HibernateUtils
2019-05-08
909422229_Hibernate缓存技术
2019-05-08
Spring、Springmvc、Springboot常用注解
2019-05-08
909422229_HTML界面自适应手机屏幕
2019-05-08
909422229_wex5打包web项目、网站等系统
2019-05-08
909422229_人工智能基础了解
2019-05-08
909422229_springboot集成swgger2
2019-05-08
909422229_SSH暴力破解与防御
2019-05-08
909422229_Android studio打包APK
2019-05-08
Android Studio获取开发版SHA1值和发布版SHA1值的史上最详细方法
2019-05-08
909422229_JAVA StringUtils需要导入的包
2019-05-08
909422229_史上最简单的HTML调用浏览器打印机方法
2019-05-08
909422229_springboot中使用poi导出Excel
2019-05-08
909422229_史上最简单的word导出,HTML直接导出,无需后台代码
2019-05-08
909422229_linux上安装mysql5.7
2019-05-08