有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。 木杆很细,不能同时通过一只蚂蚁。开始 时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。

----------------------------------------------------


***  最笨的方法当然是把蚂蚁看成一个一个的去遍历左右; // 还真有人这么写

***  然后好一点的方法是递归:

类似这种:http://blog.csdn.net/andylin02/archive/2007/07/27/1711701.aspx
就不贴代码了

***  最简单的方法当时是“禅”啦

Python代码:
def f(x):return 27-x
a=[3,7,11,17,23] 
b=map(f,a)
print 'max:',max(max(a),max(b))
print 'min:',max(min(a[x],b[x]) for x in range(0,5))


那你看,万生在人海穿梭,相遇又回头:如果把这个过程想象成我继续走,他继续走;
我穿着他的躯壳,他穿着我的躯壳,那岂不等同于我们没有相识

所以
要算最长的的时间 就是初始状况下 蚂蚁离距离它最远的那个边的最大值。
要算最少的时间 就是所有蚂蚁中最后一只走出来的时间,就是每个蚂蚁都朝着离自己近的一个端点爬,然后这些时间中最大的一个。
上面是一段简单的草稿代码 就么的注释了。

缘分那。。缘分。。




Comments

Good.Be the first to comment on this entry.

Post comment

comment has COPYRIGHT too!

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).