这个题目是以前刚学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
要求:请设计程序,输入参数--〉运行--〉得到结果:谁杀死该单位(包括自杀)随意讨论下吧,解决上班无聊时间

解决方案 »

  1.   

    第一题随便写了一下,的确很难,估计弄错了,大家指教一下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;
    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++;
    }
    }
    }
    }
      

  2.   

    如果毒素伤害不能叠加PositionThread 把run里的逻辑封装成synchronized方法
      

  3.   

    重新改了下,第一题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 - 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++;
    }
    }
    }
    }
      

  4.   

    晕,写错了伤害值,再来:
    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++;
    }
    }
    }
    }
      

  5.   

    打过魔兽,应当说魔兽是根据时间来判断杀死的,同时攻击的情况是不存在的,打过DOTA补刀的都知道。所以说每个英雄隔1.5秒打一下,两者差开来?
      

  6.   

    其实魔兽同样是按照线程的,就算同时攻击,也有弹道飞行时间的差异。就算同时攻击,同样的飞行弹道,也是根据当前线程,谁是running状态来判定杀死该单位的英雄的,玩过dota的都知道,从来不会出现一个单位同时被2个英雄杀死的情况,但是这种情况在现实中几乎很难发生。
      

  7.   

    windforcecn够耐心
    好久没玩冰封了