看到百度的面试题蚂蚁爬干,觉得听好玩的就写了一个,请高手指点
题目如下:
有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
我的出的最大时间为24妙。这道题我用的递归写的,代码如下
public class AntTheme {
private int count;//计算总共用了多少秒
private int complete; //完成的数量
private int[] ants;//蚂蚁位置
private boolean[] orientations;//蚂蚁的方向
private int longness;//竹竿长度
public AntTheme(int count,int[] ants,boolean[] orientations,int longness)
{
this.count = count;
this.complete = ants.length;
this.ants = ants;
this.orientations = orientations;
this.longness = longness;
this.doAccount(this.ants,this.orientations, this.longness);
}
public void doAccount(int[] ants,boolean[] orientations,int longness)
{
//定义蚂蚁的爬行方向
for(int index=0;index<orientations.length;index++)
{
if(orientations[index])
{
ants[index]++;
}
else
{
ants[index]--;
}
}//控制相遇时的爬行方向
for(int antIndex =0;antIndex < ants.length-1;antIndex++)
{
if(ants[antIndex] == ants[antIndex+1])
{
orientations[antIndex] = !orientations[antIndex];
orientations[antIndex+1] = !orientations[antIndex+1];
}
else
{//爬出竹竿的蚂蚁归零
if(ants[antIndex] >= longness || ants[antIndex] <= 0)
{
ants[antIndex] = 0;
this.complete--;
}
}
}
if(ants[ants.length-1] >= longness || ants[ants.length-1] <= 0)
{
ants[ants.length-1] = 0;
this.complete--;
}
if(this.complete <= 0)//是否所有的蚂蚁都爬出了竹竿
{
return;
}
else
{
System.out.println("所有蚂蚁走了" + count++ + "秒");
this.doAccount(ants, orientations, longness);//递归调用
}
}
private int complete; //完成的数量
private int[] ants;//蚂蚁位置
private boolean[] orientations;//蚂蚁的方向
private int longness;//竹竿长度
public AntTheme(int count,int[] ants,boolean[] orientations,int longness)
{
this.count = count;
this.complete = ants.length;
this.ants = ants;
this.orientations = orientations;
this.longness = longness;
this.doAccount(this.ants,this.orientations, this.longness);
}
public void doAccount(int[] ants,boolean[] orientations,int longness)
{
//定义蚂蚁的爬行方向
for(int index=0;index<orientations.length;index++)
{
if(orientations[index])
{
ants[index]++;
}
else
{
ants[index]--;
}
}//控制相遇时的爬行方向
for(int antIndex =0;antIndex < ants.length-1;antIndex++)
{
if(ants[antIndex] == ants[antIndex+1])
{
orientations[antIndex] = !orientations[antIndex];
orientations[antIndex+1] = !orientations[antIndex+1];
}
else
{//爬出竹竿的蚂蚁归零
if(ants[antIndex] >= longness || ants[antIndex] <= 0)
{
ants[antIndex] = 0;
this.complete--;
}
}
}
if(ants[ants.length-1] >= longness || ants[ants.length-1] <= 0)
{
ants[ants.length-1] = 0;
this.complete--;
}
if(this.complete <= 0)//是否所有的蚂蚁都爬出了竹竿
{
return;
}
else
{
System.out.println("所有蚂蚁走了" + count++ + "秒");
this.doAccount(ants, orientations, longness);//递归调用
}
}
public static void main(String[] args) {
//蚂蚁的爬行方向true为向右爬,false为向左爬
boolean[] orientations = {true,true,true,true,false};
//蚂蚁的位置
int[] ants = {3,7,11,17,23};
int longness = 27;//竹竿的长度
AntTheme antTheme = new AntTheme(1,ants,orientations,longness);
}
}
得出的结果:
//蚂蚁的爬行方向true为向右爬,false为向左爬
boolean[] orientations = {true,true,true,true,false};
//蚂蚁的位置
int[] ants = {3,7,11,17,23};
int longness = 27;//竹竿的长度
AntTheme antTheme = new AntTheme(1,ants,orientations,longness);
}
}
得出的结果:
所有蚂蚁走了1秒
所有蚂蚁走了2秒
所有蚂蚁走了3秒
所有蚂蚁走了4秒
所有蚂蚁走了5秒
所有蚂蚁走了6秒
所有蚂蚁走了7秒
所有蚂蚁走了8秒
所有蚂蚁走了9秒
所有蚂蚁走了10秒
所有蚂蚁走了11秒
所有蚂蚁走了12秒
所有蚂蚁走了13秒
所有蚂蚁走了14秒
所有蚂蚁走了15秒
所有蚂蚁走了16秒
所有蚂蚁走了17秒
所有蚂蚁走了18秒
所有蚂蚁走了19秒
所有蚂蚁走了20秒
所有蚂蚁走了21秒
所有蚂蚁走了22秒
所有蚂蚁走了23秒
所有蚂蚁走了24秒
所有蚂蚁走了2秒
所有蚂蚁走了3秒
所有蚂蚁走了4秒
所有蚂蚁走了5秒
所有蚂蚁走了6秒
所有蚂蚁走了7秒
所有蚂蚁走了8秒
所有蚂蚁走了9秒
所有蚂蚁走了10秒
所有蚂蚁走了11秒
所有蚂蚁走了12秒
所有蚂蚁走了13秒
所有蚂蚁走了14秒
所有蚂蚁走了15秒
所有蚂蚁走了16秒
所有蚂蚁走了17秒
所有蚂蚁走了18秒
所有蚂蚁走了19秒
所有蚂蚁走了20秒
所有蚂蚁走了21秒
所有蚂蚁走了22秒
所有蚂蚁走了23秒
所有蚂蚁走了24秒
转载于:https://blog.51cto.com/tonyaction/42036