某东笔试319

这篇博客包含两个Java程序实例。第一个程序涉及坦克与城堡的攻防逻辑,通过循环和条件判断计算攻防轮次。第二个程序处理数组数据,寻找特定条件下满足要求的最小值并记录。博客探讨了编程中的逻辑思维和算法应用。
摘要由CSDN通过智能技术生成
public class Jingdong {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();//a辆坦克
        int b = sc.nextInt();//b点HP
        int c = sc.nextInt();//每个城堡可以攻击坦克数量
        int d = sc.nextInt();//d座城堡
        int[] chengbao = new int[d];
        for (int i = 0; i < d; i++) {
            chengbao[i] = b;
        }
        int count = 0;//轮次
        int i = 0;
        int k = d;
        while (a > 0 && k > 0) {
            //攻击
            int tmpa = a;
            if (tmpa >= chengbao[i]) {
                for (; i < d; i++) {
                    if (tmpa >= chengbao[i]) {
                        tmpa -= chengbao[i];
                        chengbao[i] = 0;
                    } else break;
                }
                if (chengbao[d - 1] != 0) {
                    if (tmpa > 0) {
                        chengbao[i] -= tmpa;
                        tmpa = 0;
                    }
                }
            } else {
                chengbao[i] -= tmpa;
                tmpa = 0;
            }
            //反击
            k = d - i;
            a -= c * k;
            count++;
        }
        if (chengbao[d - 1] == 0) {
            System.out.println(count);
        } else {
            System.out.println(-1);
        }
    }
}

第一题比较简单,按照逻辑走就行。

第二题,第二个反转的K填错位置,可惜了,不然应该都能过的。

public class Jingdong2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] array = new int[m][3];
        int mass =Integer.MAX_VALUE;
        for (int k = 0; k < m; k++) {
            for (int l = 0; l < 3; l++) {
                array[k][l] = sc.nextInt();
            }
        }
        int count = 0;
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            for (int k = 0; k < m; k++) {
                if (array[k][0] == i){
                    count++;
                    mass = Math.min(array[k][2], mass);
                }else continue;
            }
            if (count==n-1) integers.add(mass);
            count = 0;
            mass = Integer.MAX_VALUE;
        }

        for (int i = 1; i <= n; i++) {
            for (int k = 0; k < m; k++) {
                if (array[k][1] == i){//这个位置提交的时候错了,麻了。
                    count++;
                    mass = Math.min(array[k][2], mass);
                }else continue;
            }
            if (count==n-1) integers.add(mass);
            count = 0;
            mass = Integer.MAX_VALUE;
        }

        Integer max = Collections.max(integers);
        System.out.println(max);
    }
}

估计凉了,给大家参考一下吧,这应该是最普通最垃圾的写法了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值