腾讯暑期实习生机试题 - 打怪兽 java

本题测试样例

4
6 2 4 9
1 1 1 2
输出:
3

4
6 2 8 9 16
1 1 1 2 2
输出:
3

3
1 1 5
1 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<monster> list1 = new ArrayList<>(); //花费为1的怪兽
            List<monster> 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<monster> {
        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;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值