腾讯暑期实习生机试题 - 打怪兽 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];            List
list1 = 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:腾讯暑期实习生机试题 - 01串 java
下一篇:redis 使用外网(公网)进行主从连接配置

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月08日 13时55分49秒