这个题目是以前刚学java的时候,做北大编程方面的题库中遇到的,当时提交人数很多,但是正确数为0,是几千的题目中唯一正确率为0%的,目前那个网站我忘记了,题目大概如下:假设情景:有一只野怪,hp=x
2个英雄同时攻击,(a英雄攻击为y),(b英雄为攻击为z,并附带毒素伤害w/s,持续2秒),攻击速度
均为1.5秒/次
要求:请设计程序,输入参数x,y,z,w--〉运行--〉得到结果:谁杀死该单位题目升级:有一只野怪,hp=x
2个英雄同时攻击,(a英雄攻击为y1-y2,弹道飞行速度为d1/100s),(b英雄为攻击为z1-z2,弹道飞行
速度为d2/100s,并附带毒素伤害w/s,持续2秒),攻击速度均为1.5秒/次
野怪自身损失hp=i/s
要求:请设计程序,输入参数--〉运行--〉得到结果:谁杀死该单位(包括自杀)随意讨论下吧,解决上班无聊时间
2个英雄同时攻击,(a英雄攻击为y),(b英雄为攻击为z,并附带毒素伤害w/s,持续2秒),攻击速度
均为1.5秒/次
要求:请设计程序,输入参数x,y,z,w--〉运行--〉得到结果:谁杀死该单位题目升级:有一只野怪,hp=x
2个英雄同时攻击,(a英雄攻击为y1-y2,弹道飞行速度为d1/100s),(b英雄为攻击为z1-z2,弹道飞行
速度为d2/100s,并附带毒素伤害w/s,持续2秒),攻击速度均为1.5秒/次
野怪自身损失hp=i/s
要求:请设计程序,输入参数--〉运行--〉得到结果:谁杀死该单位(包括自杀)随意讨论下吧,解决上班无聊时间
volatile int monsterHp = 100; int aHeroDam = 5; int bHeroDam = 4; int bHeroPositionDam = 3; int bHeroPositionContineTime = 2; long heroDamSpeed = 1500; // ms public static void main(String[] args) {
new TestWar().kill();
} public void kill() {
new KillThread("a").start();
new KillThread("b").start();
} class KillThread extends Thread {
String heroName = null; public KillThread(String _heroName) {
heroName = _heroName;
} public void run() {
while (true) {
if (heroName.equalsIgnoreCase("a")) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - aHeroDam;
if (monsterHp <= 0) {
System.out.println("a kill it");
break;
}
try {
Thread.sleep(heroDamSpeed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (heroName.equalsIgnoreCase("b")) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - bHeroPositionDam;
new PositionThread().start();
if (monsterHp <= 0) {
System.out.println("b kill it");
break;
}
try {
Thread.sleep(heroDamSpeed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} class PositionThread extends Thread { public void run() {
int i = 0;
while (bHeroPositionContineTime != i) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - bHeroPositionDam;
if (monsterHp <= 0) {
System.out.println("b kill it");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
}
volatile int monsterHp = 100; int aHeroDam = 5; int bHeroDam = 4; int bHeroPositionDam = 3; int bHeroPositionContineTime = 2; long heroDamSpeed = 1500; // ms public static void main(String[] args) {
new TestWar().kill();
} public void kill() {
new KillThread("a").start();
new KillThread("b").start();
} class KillThread extends Thread {
String heroName = null; public KillThread(String _heroName) {
heroName = _heroName;
} public void run() {
while (true) {
if (heroName.equalsIgnoreCase("a")) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - aHeroDam;
System.out.println("a kill left monster life : " + monsterHp);
if (monsterHp <= 0) {
System.out.println("a kill it");
break;
}
try {
Thread.sleep(heroDamSpeed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (heroName.equalsIgnoreCase("b")) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - bHeroPositionDam;
System.out.println("b kill left monster life : " + monsterHp);
new PositionThread().start();
if (monsterHp <= 0) {
System.out.println("b kill it");
break;
}
try {
Thread.sleep(heroDamSpeed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} class PositionThread extends Thread { public void run() {
positionKill();
} private synchronized void positionKill() {
int i = 0;
while (bHeroPositionContineTime != i) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - bHeroPositionDam;
System.out.println("b position kill left monster life : " + monsterHp);
if (monsterHp <= 0) {
System.out.println("b kill it");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
}
package test;public class TestWar {
volatile int monsterHp = 100; int aHeroDam = 5; int bHeroDam = 4; int bHeroPositionDam = 3; int bHeroPositionContineTime = 2; long heroDamSpeed = 1500; // ms public static void main(String[] args) {
new TestWar().kill();
} public void kill() {
new KillThread("a").start();
new KillThread("b").start();
} class KillThread extends Thread {
String heroName = null; public KillThread(String _heroName) {
heroName = _heroName;
} public void run() {
while (true) {
if (heroName.equalsIgnoreCase("a")) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - aHeroDam;
System.out.println("a kill left monster life : " + monsterHp);
if (monsterHp <= 0) {
System.out.println("a kill it");
break;
}
try {
Thread.sleep(heroDamSpeed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (heroName.equalsIgnoreCase("b")) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - bHeroDam;
System.out.println("b kill left monster life : " + monsterHp);
new PositionThread().start();
if (monsterHp <= 0) {
System.out.println("b kill it");
break;
}
try {
Thread.sleep(heroDamSpeed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} class PositionThread extends Thread { public void run() {
positionKill();
} private synchronized void positionKill() {
int i = 0;
while (bHeroPositionContineTime != i) {
if (monsterHp <= 0) {
break;
}
monsterHp = monsterHp - bHeroPositionDam;
System.out.println("b position kill left monster life : " + monsterHp);
if (monsterHp <= 0) {
System.out.println("b kill it");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
}
好久没玩冰封了