百度一道关于算法的面试题目
题目:有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一 只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会 同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
模拟每秒钟5只蚂蚁的情况。在生成Ant对象时确定蚂蚁的运行方向和名字。代码如下:
public class Ant {»
//蚂蚁的位置
private int position;
//爬行方向
private boolean front_flg;
//是否到达终点
private boolean isOver = false;
//蚂蚁的名字
private String antName;
public boolean isOver() {
if((position == 0)||(position == 27)){
isOver = true;
} else {
isOver = false;
}
return isOver;
}
public void setOver(boolean isOver) {
this.isOver = isOver;
}
public String getAntName() {
return antName;
}
public void setAntName(String antName) {
this.antName = antName;
}
public boolean isFront_flg() {
return front_flg;
}
public void setFront_flg(boolean front_flg) {
this.front_flg = front_flg;
}
public void setPosition(int position) {
this.position = position;
}
//构造方法
public Ant (int position,boolean flg,String antName){
this.position = position;
this.front_flg = flg;
this.antName = antName;
}
public void crawl(boolean front_flg){
//根据爬行方向判断position的加减
if(!(isOver())){
if (front_flg){
position = position + 1;
} else {
position = position - 1;
}
}
System.out.println(antName + " has arrived position : " + position);
}
public int getPosition() {
return position;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Ant ant3 = new Ant (3,true,"ant3");
Ant ant7 = new Ant (7,true,"ant7");
Ant ant11 = new Ant (11,true,"ant11");
Ant ant17 = new Ant (17,false,"ant17");
Ant ant23 = new Ant (23,false,"ant23");
//记录爬行时间
int useTime = 1;
while(!(ant3.isOver()
&&ant7.isOver()
&&ant11.isOver()
&&ant17.isOver()
&&ant23.isOver())){
//如果两只蚂蚁相遇则调转爬行方向
if (ant3.getPosition()==ant7.getPosition()){
boolean temp;
temp = ant3.isFront_flg();
ant3.setFront_flg(ant7.isFront_flg());
ant7.setFront_flg(temp);
}
if (ant3.getPosition()==ant11.getPosition()){
boolean temp;
temp = ant3.isFront_flg();
ant3.setFront_flg(ant11.isFront_flg());
ant11.setFront_flg(temp);
}
if (ant3.getPosition()==ant17.getPosition()){
boolean temp;
temp = ant3.isFront_flg();
ant3.setFront_flg(ant17.isFront_flg());
ant17.setFront_flg(temp);
}
if (ant3.getPosition()==ant23.getPosition()){
boolean temp;
temp = ant3.isFront_flg();
ant3.setFront_flg(ant23.isFront_flg());
ant23.setFront_flg(temp);
}
if (ant7.getPosition()==ant11.getPosition()){
boolean temp;
temp = ant7.isFront_flg();
ant7.setFront_flg(ant11.isFront_flg());
ant11.setFront_flg(temp);
}
if (ant7.getPosition()==ant17.getPosition()){
boolean temp;
temp = ant7.isFront_flg();
ant7.setFront_flg(ant17.isFront_flg());
ant17.setFront_flg(temp);
}
if (ant7.getPosition()==ant23.getPosition()){
boolean temp;
temp = ant7.isFront_flg();
ant7.setFront_flg(ant23.isFront_flg());
ant23.setFront_flg(temp);
}
if (ant11.getPosition()==ant17.getPosition()){
boolean temp;
temp = ant11.isFront_flg();
ant11.setFront_flg(ant17.isFront_flg());
ant17.setFront_flg(temp);
}
if (ant11.getPosition()==ant23.getPosition()){
boolean temp;
temp = ant11.isFront_flg();
ant11.setFront_flg(ant23.isFront_flg());
ant23.setFront_flg(temp);
}
if (ant17.getPosition()==ant23.getPosition()){
boolean temp;
temp = ant17.isFront_flg();
ant17.setFront_flg(ant23.isFront_flg());
ant23.setFront_flg(temp);
}
//蚂蚁爬行
ant3.crawl(ant3.isFront_flg());
ant7.crawl(ant7.isFront_flg());
ant11.crawl(ant11.isFront_flg());
ant17.crawl(ant17.isFront_flg());
ant23.crawl(ant23.isFront_flg());
System.out.println("Use time is :" + useTime++);
System.out.println("================================");
}
}
}