拼多多编程题求问
关于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]);
}
}
}
}
#拼多多##笔试题目#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]);
}
}
}
}