拼多多编程题求问

关于HP游戏那道题,请问为什么一直是75%,报的是数组越界问题
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int HP = in.nextInt();
int normalAttack = in.nextInt();
int buffedAttack = in.nextInt();
if(normalAttack > buffedAttack || normalAttack * 2 > buffedAttack) {
if(HP % normalAttack == 0) {
System.out.println(HP / normalAttack);
}
else {
System.out.println(HP / normalAttack + 1);
}
}
else if (HP <= normalAttack) {
System.out.println(1);
}
else if (HP <= buffedAttack) {
System.out.println(2);
}
else {
int[] res = new int[HP + 1];
for (int i = 1; i <= normalAttack; i++) {
res[i] = 1;
}
for (int i = normalAttack + 1; i <= buffedAttack; i++) {
res[i] = 2;
}
for (int i = buffedAttack + 1; i <= HP; i++) {
res[i] = Math.min(res[i - normalAttack] + 1, res[i - buffedAttack] + 2);
}
System.out.println(res[HP]);
}
}

}
}

#拼多多##笔试题目#
全部评论
根据hp,普通攻击,buff攻击之间的关系写if else解析式就可以了。。
点赞 回复
分享
发布于 2018-08-30 21:46
buff攻击大于二倍平A的时候,只需要特殊考虑最后一攻就可以,其余都是buff攻击
点赞 回复
分享
发布于 2018-08-30 22:00
联易融
校招火热招聘中
官网直投
//没那么麻烦把!写的比较low import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int hp=sc.nextInt(); int normal=sc.nextInt(); int bf=sc.nextInt(); int count=0; if(2*normal>=bf){ count=hp%normal==0? hp/normal:hp/normal+1; } else{ int i=hp/bf; if(hp/bf!=0) { if (hp - bf * i 1; else count += 2; } count+=i; } System.out.println(count); } }
点赞 回复
分享
发布于 2018-08-30 22:15
贪心不就好了。
点赞 回复
分享
发布于 2018-08-30 23:59

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务