# 如何优化运行速度

chdaxia 2015-09-05 01:26:07

si: 初始法力。

mi: 最大法力上限。

ri: 每秒中法力回复速度。

``````import java.util.Scanner;

public class Main {

public class Magician {
public int mana;
public int maxMana;
public int regenerateNum;
public int timeT;

Magician(int mana, int maxMana, int regenerateNum) {
this.mana = mana;
this.maxMana = maxMana;
this.regenerateNum = regenerateNum;
timeT = 0;
}

public int absorbMana(int currentTime) {
int result = 0;
result = mana + (currentTime - timeT) * regenerateNum < maxMana ? mana
+ (currentTime - timeT) * regenerateNum
: maxMana;
// System.out.println(result);
timeT = currentTime;
mana = 0;
return result;
}

}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int numOfMagician = sc.nextInt();
Magician magicianList[] = new Magician[numOfMagician];

int sj, mj, rj;
Main test = new Main();
for (int i = 0; i < numOfMagician; i++) {
sj = sc.nextInt();
mj = sc.nextInt();
rj = sc.nextInt();
magicianList[i] = test.new Magician(sj, mj, rj);
}

int numOfOperation = sc.nextInt();
int ti, li, ri;
int result = 0;
for (int i = 0; i < numOfOperation; i++) {
ti = sc.nextInt();
li = sc.nextInt() - 1;
ri = sc.nextInt() - 1;
while (li <= ri) {
result += magicianList[li].absorbMana(ti);
li++;
// System.out.println("absorb  "+li+"result is "+result);
}
}

System.out.println(result);

}
}
``````

...全文
62 回复 打赏 收藏 转发到动态 举报

50,454

javaspring bootspring cloud 技术论坛（原bbs）

• 近7日
• 近30日
• 至今