原题:
有一根27厘米长的木杆,在第3、7、11、17、23厘米处各有一只蚂蚁。木杆只能通过一只蚂蚁。开始时,蚂蚁的爬行方向随机,但它们在遇到另外一只蚂蚁之前,只能想前爬,不能回头。当任意两只蚂蚁相遇时,两只蚂蚁将同时调头往相反的方向前进。
问:所有蚂蚁离开木杆的最短和最长时间是多少?
===================================
java代码:
package
chan.puzzle;
import java.util.ArrayList;
import java.util.Iterator;
import chan.sort.Sort;
public class AntRun ... {
/** *//**
* 一根棍子上有若干蚂蚁,可以往各个方向爬;
* 问左右蚂蚁最快和最慢爬下棍子的时间
*/
public static void main(String[] args) ...{
final int STICK = 27;//棍子长度
final int[] a = ...{3,7,11,17,23};//蚂蚁初试位置
int x = (int)Math.pow(2, a.length);//总共的可能性
String[] direct = new String[x];
int[] times = new int[x];
ArrayList<Ant> ants = new ArrayList<Ant>();
for(int i: times)
i = 0;
//用一个a.length位的二进制数来标识蚂蚁的行动方向
for(int i=0;i<x;i++) ...{
String str = Integer.toString(i, 2);
StringBuffer sb = new StringBuffer();
for(int j=0;j<a.length-str.length();j++)
sb.append("0");
sb.append(str);
direct[i] = sb.toString();
}
for(int i=0;i<x;i++) ...{
ants.clear();
for(int k=0;k<a.length;k++) ...{
ants.add(new Ant(a[k]));
ants.get(k).direct = (direct[i].charAt(k)=='1')? 1 : -1 ;
}
//ants run!
while(!(ants.isEmpty())) ...{
ant_run(ants);
times[i]++;
changeway(ants);
complete(ants, STICK);
}
print("第"+(i+1)+"次所用的时间: " + times[i]);
}//for
print("==============================");
//找出max和min
Sort sort = new Sort();
sort.setArray(times);
sort.qSort(0, times.length-1);
print("花费最大时间为: "+times[0]);
print("花费最小时间为: "+times[times.length-1]);
}//main
public static void changeway(ArrayList<Ant> list) ...{
for(int i=0;i<list.size()-1;i++) ...{
if(list.get(i).position==list.get(i+1).position) ...{
list.get(i).turn_around();
list.get(i+1).turn_around();
}
}
}//changeway
public static void complete(ArrayList<Ant> list, int goal) ...{
Iterator iter = list.iterator();
while(iter.hasNext()) ...{
Ant ant = (Ant)iter.next();
if((ant.position==0)||(ant.position==goal))
// list.remove(ant);
iter.remove();
}
}//complete
public static void ant_run(ArrayList<Ant> list) ...{
for(Ant ant: list)...{
ant.run();
}
}//ant_run
public static void print(Object o) ...{
System.out.println(o);
}//print
} // AntRun
class Ant ... {
int direct ; //初始方向, 1, -1
int position; //初始位置 0 - 27
public Ant(int position) ...{
this.position = position;
}//Ant
public void run() ...{
position+=direct;
}//run
public void turn_around() ...{
direct *= -1;
}
} // Ant
import java.util.ArrayList;
import java.util.Iterator;
import chan.sort.Sort;
public class AntRun ... {
/** *//**
* 一根棍子上有若干蚂蚁,可以往各个方向爬;
* 问左右蚂蚁最快和最慢爬下棍子的时间
*/
public static void main(String[] args) ...{
final int STICK = 27;//棍子长度
final int[] a = ...{3,7,11,17,23};//蚂蚁初试位置
int x = (int)Math.pow(2, a.length);//总共的可能性
String[] direct = new String[x];
int[] times = new int[x];
ArrayList<Ant> ants = new ArrayList<Ant>();
for(int i: times)
i = 0;
//用一个a.length位的二进制数来标识蚂蚁的行动方向
for(int i=0;i<x;i++) ...{
String str = Integer.toString(i, 2);
StringBuffer sb = new StringBuffer();
for(int j=0;j<a.length-str.length();j++)
sb.append("0");
sb.append(str);
direct[i] = sb.toString();
}
for(int i=0;i<x;i++) ...{
ants.clear();
for(int k=0;k<a.length;k++) ...{
ants.add(new Ant(a[k]));
ants.get(k).direct = (direct[i].charAt(k)=='1')? 1 : -1 ;
}
//ants run!
while(!(ants.isEmpty())) ...{
ant_run(ants);
times[i]++;
changeway(ants);
complete(ants, STICK);
}
print("第"+(i+1)+"次所用的时间: " + times[i]);
}//for
print("==============================");
//找出max和min
Sort sort = new Sort();
sort.setArray(times);
sort.qSort(0, times.length-1);
print("花费最大时间为: "+times[0]);
print("花费最小时间为: "+times[times.length-1]);
}//main
public static void changeway(ArrayList<Ant> list) ...{
for(int i=0;i<list.size()-1;i++) ...{
if(list.get(i).position==list.get(i+1).position) ...{
list.get(i).turn_around();
list.get(i+1).turn_around();
}
}
}//changeway
public static void complete(ArrayList<Ant> list, int goal) ...{
Iterator iter = list.iterator();
while(iter.hasNext()) ...{
Ant ant = (Ant)iter.next();
if((ant.position==0)||(ant.position==goal))
// list.remove(ant);
iter.remove();
}
}//complete
public static void ant_run(ArrayList<Ant> list) ...{
for(Ant ant: list)...{
ant.run();
}
}//ant_run
public static void print(Object o) ...{
System.out.println(o);
} // AntRun
class Ant ... {
int direct ; //初始方向, 1, -1
int position; //初始位置 0 - 27
public Ant(int position) ...{
this.position = position;
}//Ant
public void run() ...{
position+=direct;
}//run
public void turn_around() ...{
direct *= -1;
}
} // Ant
======================
注:本人不才,写的代码比较乱,且存在好多不合理之处。并,刚写完测试运行过就迫不及待的发来这里,并没有来得及进行优化。还请高手斧正!
声明:虽然代码不是很美观,但也是本人辛苦了一个上午的劳动成果。如转载,请著名原出处!谢谢!