首页 > 试题广场 >

回合制游戏

[编程题]回合制游戏
  • 热度指数:7321 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
你在玩一个回合制角色扮演的游戏。现在你在准备一个策略,以便在最短的回合内击败敌方角色。在战斗开始时,敌人拥有HP格血量。当血量小于等于0时,敌人死去。一个缺乏经验的玩家可能简单地尝试每个回合都攻击。但是你知道辅助技能的重要性。
在你的每个回合开始时你可以选择以下两个动作之一:聚力或者攻击。
    聚力会提高你下个回合攻击的伤害。
    攻击会对敌人造成一定量的伤害。如果你上个回合使用了聚力,那这次攻击会对敌人造成buffedAttack点伤害。否则,会造成normalAttack点伤害。
给出血量HP和不同攻击的伤害,buffedAttack和normalAttack,返回你能杀死敌人的最小回合数。

输入描述:
第一行是一个数字HP
第二行是一个数字normalAttack
第三行是一个数字buffedAttack
1 <= HP,buffedAttack,normalAttack <= 10^9


输出描述:
输出一个数字表示最小回合数
示例1

输入

13
3
5

输出

5
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int hp = in.nextInt();
            int na = in.nextInt();
            int ba = in.nextInt();
            //
            boolean flag = false;
            if (ba > 2 * na)flag = true;
            // 如果蓄力两回合还不如不蓄力
            if (!flag) {
                int n = hp / na;
                int m = hp % na;
                if (m > 0) {
                    n++;
                }
                System.out.print(n);
            } else {
                for (int i = 1;; i++) {
                    if (i % 2 == 1) {
                        if (hp <= na) {
                            System.out.print(i);
                            break;
                        } else {
                            hp -= 0;
                        }
                    } else {
                        hp -= ba;
                    }
                    if (hp <= 0) {
                        System.out.print(i);
                        break;
                    }
                }
            }
        }
    }
}

发表于 2023-11-30 01:06:25 回复(0)
本人擅长各类if else语句
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int hp = sc.nextInt();
        int na = sc.nextInt();
        int ba = sc.nextInt();
        if(ba >= 2 * na){
            int ans = hp / ba;
            int remain = hp % ba;
            if(remain == 0){
                System.out.println(2*ans);
                return;
            }
            else if(remain <= na){
                System.out.println(2*ans+1);
                return;
            }
            else{
                System.out.println(2*ans+2);
                return;
            }
        }else{
            int ans = hp % na == 0 ? hp/na : hp / na + 1;
            System.out.println(ans);
        }
    }
}

发表于 2020-04-21 20:56:45 回复(0)

刚刚写的博客,同步到牛客一下~https://blog.csdn.net/weixin_39350124/article/details/91345870

解法1(深度优先搜索)

(case通过率只有75%,想AC的可以直接看解法2)
用一个int变量buffed,当buffed=1表示buffedAttack,buffed=0表示normalAttack,然后每次进入递归方法都有两种决策方式。具体Code如下所示:

import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static HashMap<String, Integer> map = new HashMap<>(); //用于记忆化搜索

    /**
     * @param hp 当前hp
     * @param normalAttack 普通攻击
     * @param buffedAttack 辅助攻击
     * @param isBuffed 本回合是否使用辅助攻击
     * @param cur 当前回合数
     * @return
     */
    public static int process(int hp, int normalAttack, int buffedAttack, int isBuffed, int cur) {
        String str = isBuffed + "_" + hp; //用isBuffed和hp作为记忆化搜索的key

        //每次进入递归之前先搜索是否有缓存结果
        if (map.containsKey(str)) {
            return map.get(str);
        }
        //如果hp < 1,返回需要的回合
        if (hp < 1) {
            map.put(str, cur); //返回结果前加缓存
            return cur;
        }

        int res = 0;
        if (isBuffed == 1) {
            //如果上回合用buffedAttack,则这回合直接减去敌人生命值
            res = process(hp - buffedAttack, normalAttack, buffedAttack, 0, cur + 1);
        } else {
            //如果上回合使用normalAttack,则这回合有两种决策方式
            res = Math.min(process(hp - normalAttack, normalAttack, buffedAttack, 0, cur + 1),
                    process(hp, normalAttack, buffedAttack, 1, cur + 1));
        }
        map.put(str, res); //返回结果前加缓存
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int hp = sc.nextInt();
        int normal = sc.nextInt();
        int buffed = sc.nextInt();
        //一开始有两种选择
        int a = process(hp, normal, buffed, 1, 1); //蓄力
        int b = process(hp - normal, normal, buffed, 0, 1); //普通攻击
        System.out.println(Math.min(a, b));
    }
}

解法2(数学规律)

直接用数学规律去分析:
1)如果buffedAttack <= 2 * normalAttack,那么一定全程使用normalAttack更划算
2)如果buffedAttack > 2 * normalAttack,那么优先使用buffedAttack更划算,如果hp % normalAttack > 0,当余数小于normalAttack,最后一回合用normalAttack就能杀死敌人;如果hp % normalAttack == 0,那么一定全程使用buffedAttack更划算。
具体Code如下:

import java.util.Scanner;

public class Main {

    public static int func(int hp, int normalAttack, int buffedAttack) {
        int res = 0;
        /**
         * 如果buffedAttack > 2 * normalAttack,那么一定优先使用buffedAttack划算
         */
        if (buffedAttack > 2 * normalAttack) {
            res = hp % buffedAttack;
            /**
             * 如果hp % normalAttack > 0
             * 当余数小于normalAttack,最后一回合用normalAttack就能杀死敌人
             * 其它情况,一直使用buffedAttack攻击
             */
            if (res > 0 && res <= normalAttack) {
                res = (hp / buffedAttack) * 2 + 1;
            } else {
                res = ((hp - 1) / buffedAttack) * 2 + 2;
            }
        } else {
            /**
             * 如果buffedAttack <= 2 * normalAttack,一直单回合普通攻击更划算
             */
            res = (hp - 1) / normalAttack + 1;
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int hp = sc.nextInt();
        int normal = sc.nextInt();
        int buffed = sc.nextInt();
        System.out.println(func(hp, normal, buffed));
    }
}
发表于 2019-06-08 16:55:54 回复(4)