一根长30厘米的棍子,在4厘米,7厘米,11厘米,17厘米23厘米上
各自放了一只蚂蚁,蚂蚁的头朝哪边是不一定的。
蚂蚁沿着棍子走动,两只蚂蚁一旦相撞之后就会朝着各自的相反方向爬去。
问:编程求得所有的蚂蚁离开棍子的最短时间和最长时间。

解决方案 »

  1.   

    不好意思,少写了两个条件:
    蚂蚁的爬行速度是1cm/s,并且杆子很细,只能同时通过一只蚂蚁。
      

  2.   

    // public class Sample10 {
    // public static void main(String[] args) {
    // final int ant[] = { 4, 7, 11, 17, 23 };
    // int direction[] = new int[ant.length];
    // int newdirection[] = new int[ant.length];
    // int time = 0;
    // int count = 0;
    // for (direction[0] = 0; direction[0] < 2; direction[0]++) {
    //
    // for (direction[1] = 0; direction[1] < 2; direction[1]++) {
    //
    // for (direction[2] = 0; direction[2] < 2; direction[2]++) {
    //
    // for (direction[3] = 0; direction[3] < 2; direction[3]++) {
    //
    // for (direction[4] = 0; direction[4] < 2; direction[4]++) {
    // time = 0;
    // count++;
    //
    // for (int i = 0; i < ant.length; i++) {
    // newdirection[i] = direction[i];
    // }
    // LeftOrRight startant = new LeftOrRight(ant);
    // for (;;) {
    //
    // for (int i = 0; i < 5; i++) {
    // for (int j = i + 1; j < 5; j++) {
    // if (startant.newant[i] == startant.newant[j]) {
    // if (newdirection[i] == 0)
    // newdirection[i] = 1;
    // else
    // newdirection[i] = 0;
    // if (newdirection[j] == 0)
    // newdirection[j] = 1;
    // else
    // newdirection[j] = 0;
    // }
    // }
    // }
    // startant.leftorright(newdirection);
    // if ((startant.newant[0] < 0 || startant.newant[0] > 30)
    // && (startant.newant[1] < 0 || startant.newant[1] > 30)
    // && (startant.newant[2] < 0 || startant.newant[2] > 30)
    // && (startant.newant[3] < 0 || startant.newant[3] > 30)
    // && (startant.newant[4] < 0 || startant.newant[4] > 30))
    // break;
    // time++;
    // }
    // System.out.println(count + ":" + time);
    // }
    // }
    // }
    // }
    // }
    // }
    // }
    // public class LeftOrRight {
    // int newant[] = new int[5];
    // public LeftOrRight(int ant[]) {
    // for(int i=0;i<ant.length;i++){
    // newant[i] = ant[i];
    // }
    // }
    // public void leftorright(int a[]) {
    // for (int i = 0; i < a.length; i++) {
    // if (a[i] == 0)
    // newant[i]++;
    // else
    // newant[i]--;
    // }
    // }
    // }
      

  3.   

    这个题的特点是:
    “两只蚂蚁一旦相撞之后就会朝着各自的相反方向爬去“ 和“所有蚂蚁的速度相同“
    所以假设 蚂蚁A和蚂蚁B相对而行,
    TMP(方向)= 蚂蚁A(方向);
    蚂蚁A(方向)=蚂蚁B(方向);
    蚂蚁B(方向)=TMP(方向);
    就可以看出其实每只蚂蚁都没有改变方向,而是一直朝着他的初始方向一直运行着,
    所以就得出了4楼的结论。
      

  4.   

    不晓得怎么算的哈 假设得了一个最小值 3只蚂蚁(4 7 11cm)头的方向都是向外 17 23cm处2只蚂蚁方向相反。。时间会是多少? 11s!!!不知道说得有道理没!
      

  5.   

    顶,经典题。// 蚂蚁。
    class Ant { // 蚂蚁距离棍子左端点的距离。
    private int left;
    // 爬行速度。
    private int cm_s; // cm/s
    // 爬行方向。
    private int way; public int getLeft() {
    return left;
    } public void setLeft(int left) {
    this.left = left;
    } public int getCm_s() {
    return cm_s;
    } public void setCm_s(int cm_s) {
    this.cm_s = cm_s;
    } public int getWay() {
    return way;
    } public void setWay(int antOperateWay) {
    way = antOperateWay;
    }
    }// 棍子
    class Truncheon { // 附着在棍子上的蚂蚁。
    private Ant[] ants;
    // 棍子的长度。
    private int len; public Truncheon(int len) {
    this.len = len;
    } public Ant[] getAnts() {
    return ants;
    } public void setAnts(Ant[] ants) {
    this.ants = ants;
    } public int getLen() {
    return len;
    }
    }// 控制蚂蚁行动的类。
    class AntOperation { public final static int LEFT = 0;
    public final static int RIGHT = 1; // 作为实验用的棍子。
    private Truncheon truncheon; public Truncheon getTruncheon() {
    return truncheon;
    } public void setT(Truncheon truncheon) {
    this.truncheon = truncheon;
    } // 棍子上还有蚂蚁。
    public boolean haveAnt(Ant[] ants, int len) {
    for (int i = 0; i < ants.length; i++) {
    Ant ant = ants[i];
    int left = ant.getLeft();
    if ((left >= 0 && left <= len) && ants.length == i + 1) {
    return true;
    }
    } return false;
    } // 让蚂蚁在棍子上向左或向右爬行。(蚂蚁移动的距离=速度×运行的时间)
    public void operate(int second) {
    Ant[] ants = truncheon.getAnts();
    for (int i = 0; i < ants.length; i++) {
    // 当前蚂蚁。
    Ant ant = ants[i];
    // 位置。
    int left = ant.getLeft();
    // 爬行速度。
    int cm_s = ant.getCm_s(); switch (ant.getWay()) {
    case LEFT:
    ant.setLeft(left - cm_s * second);
    break;
    case RIGHT:
    ant.setLeft(left + cm_s * second);
    break;
    }
    }
    }
    }// 最长时间
    // 与最短时间情况相反。
    class LongTime implements Runnable { private AntOperation ao; LongTime(AntOperation ao) {
    this.ao = ao;
    } public void run() {
    // 与最短时间情况相反。略。
    }
    }// 最短时间
    // 要求得蚂蚁离开棍子的最短时间,那么只有当蚂蚁朝左走且距离左端点距离大于
    // 右端点的距离,尽可能与其它蚂蚁相撞。或者当蚂蚁朝左走且距离左端点距离小于
    // 右端点的距离,尽可能不与其它蚂蚁相撞;当蚂蚁朝右走且距离左端点距离大于
    // 右端点的距离,尽可能不与其它蚂蚁相撞。或者当蚂蚁朝右走且距离左端点距离小于
    // 右端点的距离,尽可能与其它蚂蚁相撞。
    class ShortTime implements Runnable { private AntOperation ao; // 记录时长用的秒。
    private static int second = 0; ShortTime(AntOperation ao) {
    this.ao = ao;
    } public void run() {
    Truncheon truncheon = ao.getTruncheon();
    Ant[] ants = truncheon.getAnts();
    // 棍子长度。
    int len = truncheon.getLen(); // 棍子上还有蚂蚁。
    while (ao.haveAnt(ants, len)) { // 对每只蚂蚁爬行方向进行修改。 (中间的过程由楼主完成)
    for (int i = 0; i < ants.length; i++) {
    /***************************************************************
     * int left = ants[i].getLeft(); // 蚂蚁是否朝左走。 boolean isLeft =
     * false; if (left > len / 2) { if (!isLeft) { // 令右边的蚂蚁都向右走。 }
     * else { // 令左边的蚂蚁都向右走。 } } else { if (!isLeft) { //
     * 令右边的蚂蚁都向左走。 } else { // 令左边的蚂蚁都向左走。 } }
     **************************************************************/
    } // 让它们动起来。
    ao.operate(second);
    } // 输出需要的时间。
    System.out.println(second);
    }
    }public class AntTest { private Truncheon truncheon; AntOperation getAntOperation() {
    /** ***************** 初始化 ****************** */
    AntOperation ao = new AntOperation(); // 将棍子作为备用。
    ao.setT(truncheon); return ao;
    } public static void main(String[] args) {
    AntTest at = new AntTest(); // 拿一根30cm长的棍子,作为蚂蚁行动的轨迹。
    Truncheon truncheon = new Truncheon(30); // 蚂蚁的起始位置(距离左端点的位置)。
    int[] lefts = { 4, 7, 11, 17, 23 };
    // 蚂蚁的爬行速度(单位cm/s)。
    int[] cm_s = { 1, 1, 1, 1, 1 };
    // 确定5只蚂蚁。
    Ant[] ants = new Ant[5];
    for (int i = 0; i < ants.length; i++) {
    ants[i] = new Ant();
    ants[i].setLeft(lefts[i]);
    ants[i].setCm_s(cm_s[i]);
    } // 让蚂蚁各就位于棍子。
    truncheon.setAnts(ants); // ...
    at.setTruncheon(truncheon); AntOperation ao = at.getAntOperation(); /** ***************** 正常时间 ****************** */
    // 分析: 每只蚂蚁距离棍子左端点的距离小于0或者大于30,一旦有蚂蚁位置相同,
    // 说明两只蚂蚁相遇,然后将每只蚂蚁距离左端点的位置取反。以此类推,直到所
    // 有蚂蚁距离棍子左端点的距离都小于0或者大于30为止。
    // 略。
    /** ***************** 最短时间 ****************** */
    Thread run1 = new Thread(new ShortTime(ao));
    run1.start(); try {
    run1.join();
    } catch (InterruptedException ex) {
    System.err.println(ex.getMessage());
    } /** ***************** 最长时间 ****************** */
    Thread run2 = new Thread(new LongTime(ao));
    run2.start();
    } public Truncheon getTruncheon() {
    return truncheon;
    } public void setTruncheon(Truncheon truncheon) {
    this.truncheon = truncheon;
    }
    }
    http://topic.csdn.net/u/20081130/15/2ee11370-774f-4e7f-9700-d22116b2e2dd.html
      

  6.   

    其实这个题的答案很简单无论是最长时间(就是距离左端或者右端的最远的蚂蚁用的时间,因为蚂蚁相遇时候,实际上没有发生任何变化,只是蚂蚁的实例不一样(hashcode不一样),对于整体而言没有变化,因为没有对abcde5只蚂蚁做标记,目前5只蚂蚁是一样的,没有名字,没有个性,所以相遇换不换方向后者是不换任何方向(想象成可以穿过)是一样的没有任何区别。30-4=26),还是最短时间(每个蚂蚁找到离两端最近的一端开始走。。那么其中离得最远的哪一只的所需的时间就是最短时间==30-17)回过头来看题目:
    一根长30厘米的棍子,在4厘米,7厘米,11厘米,17厘米23厘米上 
    各自放了一只蚂蚁,蚂蚁的头朝哪边是不一定的。 
    蚂蚁沿着棍子走动,两只蚂蚁一旦相撞之后就会朝着各自的相反方向爬去。 
    问:编程求得所有的蚂蚁离开棍子的最短时间和最长时间。
    max(4-0,7-0,11-0,30-17,30-23)求最小时间=13
    max(30-4,30-7,30-11,17-0,23-0)求最大时间=26那么有人会说,假如棍子是1000厘米,其中有100个蚂蚁,在棍子的不同的位置,并且出发的方向也是不一样的,求大的时间好最小的时间。。怎么得出答案呢?其实按照上面的思想很简单
    不妨设;1,棍子的长度为单位为total厘米,棍子上有n个蚂蚁。每个蚂蚁所在是位置为x(0<x<total)厘米
    那么首先计算其中的一个蚂蚁走出这个杆的最小的时间为:它到两端中最短的那一端距离所花的时间= min_time
    min_time=min_lucheng(路程)/sudu(速度1/cm)
    min_lucheng= x <=total/2?x:(total-x)然后找出所有的蚂蚁的按照最短的时间中哪个需要时间最长的,那么这个蚂蚁所用的时间就是最短的时间(话有点绕口,多读几遍)最短的时间 =  max(蚂蚁1(min_time),蚂蚁2(min_time),蚂蚁3(min_time),,,,,蚂蚁n(min_time))同理最长时间的哪个蚂蚁
    那么首先计算其中的一个蚂蚁走出这个杆的最大的时间为:它到两端中最短的那一端距离所花的时间= max_time
    max_time=max_lucheng(路程)/sudu(速度1/cm)
    max_lucheng= x <=total/2?(total-x):x
    最长的时间 =  max(蚂蚁1(max_time),蚂蚁2(max_time),蚂蚁3(max_time),,,,,蚂蚁n(max_time))用简短的程序就可以判断出来。
    那么提出一个新的需求::
    如果两个蚂蚁反向以后,新方向向左的蚂蚁速度加(x)倍,新方向向右的蚂蚁速度减慢(y)倍这个时候。呢??上面就不满足了把这个问题。继续留给后面的朋友。。
      

  7.   

    // public class Sample10 { 
    // public static void main(String[] args) { 
    // final int ant[] = { 4, 7, 11, 17, 23 }; 
    // int direction[] = new int[ant.length]; 
    // int newdirection[] = new int[ant.length]; 
    // int time = 0; 
    // int count = 0; 
    // for (direction[0] = 0; direction[0] < 2; direction[0]++) { 
    // 
    // for (direction[1] = 0; direction[1] < 2; direction[1]++) { 
    // 
    // for (direction[2] = 0; direction[2] < 2; direction[2]++) { 
    // 
    // for (direction[3] = 0; direction[3] < 2; direction[3]++) { 
    // 
    // for (direction[4] = 0; direction[4] < 2; direction[4]++) { 
    // time = 0; 
    // count++; 
    // 
    // for (int i = 0; i < ant.length; i++) { 
    // newdirection[i] = direction[i]; 
    // } 
    // LeftOrRight startant = new LeftOrRight(ant); 
    // for (;;) { 
    // 
    // for (int i = 0; i < 5; i++) { 
    // for (int j = i + 1; j < 5; j++) { 
    // if (startant.newant[i] == startant.newant[j]) { 
    // if (newdirection[i] == 0) 
    // newdirection[i] = 1; 
    // else 
    // newdirection[i] = 0; 
    // if (newdirection[j] == 0) 
    // newdirection[j] = 1; 
    // else 
    // newdirection[j] = 0; 
    // } 
    // } 
    // } 
    // startant.leftorright(newdirection); 
    // if ((startant.newant[0] < 0 || startant.newant[0] > 30) 
    // && (startant.newant[1] < 0 || startant.newant[1] > 30) 
    // && (startant.newant[2] < 0 || startant.newant[2] > 30) 
    // && (startant.newant[3] < 0 || startant.newant[3] > 30) 
    // && (startant.newant[4] < 0 || startant.newant[4] > 30)) 
    // break; 
    // time++; 
    // } 
    // System.out.println(count + ":" + time); 
    // } 
    // } 
    // } 
    // } 
    // } 
    // } 
    // } 
    // public class LeftOrRight { 
    // int newant[] = new int[5]; 
    // public LeftOrRight(int ant[]) { 
    // for(int i=0;i <ant.length;i++){ 
    // newant[i] = ant[i]; 
    // } 
    // } 
    // public void leftorright(int a[]) { 
    // for (int i = 0; i < a.length; i++) { 
    // if (a[i] == 0) 
    // newant[i]++; 
    // else 
    // newant[i]--; 
    // } 
    // } 
    // }