原题:
Problem
某企业面试编程题:蚂蚁爬杆
有一根300厘米的细木杆,在第30厘米、80厘米、110厘米、160厘米、250厘米这五个位置上各有一只蚂蚁。
木杆很细,不能同时通过两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。
当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝相反方向走。假设蚂蚁们每秒钟可以走5厘米的距离。
请编写一个程序,计算各种可能情形下所有蚂蚁都离开木杆的最小时间和最大时间。

我这个题目做了两次,一次是用JS实现滴,一次是用C#实现的
js的实现类分析不明确,没能挖掘出隐藏的类,所以感觉很乱,后面的C#版本有很大的改进,
但是后面涉及图形的演示的时候,又发现自己设计有遗漏:

现在 把代码发上来以后可以看看 还有可以修改的地方吧!
js:
  1 function walk()
  2 {//debugger;
  3     this._pos += this._dir*this._speed;
  4 }
  5 function whenMeet()
  6 {
  7     this._dir = -1*this._dir;
  8 }
  9 function subMakeAnts(pos)
 10 {
 11     var _res = new Array();
 12     var _ant_1 = new ants();
 13     _ant_1._pos = pos;
 14     _ant_1._dir = 1;
 15     _res.push(_ant_1);
 16     var _ant_2 = new ants();
 17     _ant_2._pos = pos;
 18     _ant_2._dir = -1;
 19     _res.push(_ant_2);
 20     
 21     return _res;
 22 }
 23 function makeAntGroup(tempAnts,indexs)
 24 {
 25     var _res = new Array();
 26     _res.push(tempAnts[0][indexs[0]]);
 27     _res.push(tempAnts[1][indexs[1]]);
 28     _res.push(tempAnts[2][indexs[2]]);
 29     _res.push(tempAnts[3][indexs[3]]);
 30     _res.push(tempAnts[4][indexs[4]]);
 31     return _res;
 32 }
 33 function makeTempAnts()
 34 {
 35     var _pos = new Array();
 36     _pos.push(60);
 37     _pos.push(160);
 38     _pos.push(220);
 39     _pos.push(320);
 40     _pos.push(500);
 41     var _tempAnts = new Array();
 42     for(var i=0; i<_pos.length; i++)
 43     {
 44         _tempAnts.push(subMakeAnts(_pos[i]));
 45     }
 46     return _tempAnts;
 47 }
 48 function makeAnts()
 49 {
 50     var _tempAnts = makeTempAnts();
 51     var _res = new Array();
 52     for(var a=0; a<2; a++ )
 53     {
 54         for(var b=0; b<2; b++ )
 55         {
 56             for(var c=0; c<2; c++ )
 57             {
 58                 for(var d=0; d<2; d++ )
 59                 {
 60                     for(var e=0; e<2; e++ )
 61                     {
 62                         var _indexs = new Array();
 63                         _indexs.push(a);
 64                         _indexs.push(b);
 65                         _indexs.push(c);
 66                         _indexs.push(d);
 67                         _indexs.push(e);
 68                         _res.push(makeAntGroup(_tempAnts,_indexs));
 69                     }
 70                 }
 71             }
 72         }
 73     }
 74     //alert(_res.length);//must32
 75     return _res;
 76 }
 77 
 78 function isLeave()
 79 {
 80     if(this._pos<=0 || this._pos>=600)
 81     {
 82         return true;
 83     }
 84     return false;
 85 }
 86 
 87 function ants()
 88 {
 89     this._pos = 0;
 90     this._dir = 1;
 91     this._speed = 1;
 92     this.walk = walk;
 93     this.whenMeet = whenMeet;
 94     this.makeAnts = makeAnts;
 95     this.isLeave = isLeave;
 96 }
 97 /*-----------stick-------------*/
 98 function makeSticks(ants)
 99 {
100     var _res = new Array();
101     var _stick;
102     for(var i=0; i<ants.length; i++)
103     {
104         _stick = new sticks();
105         _stick._ants = ants[i];
106         _res.push(_stick);
107     }
108     return _res;
109 }
110 
111 function judgeMeet()
112 {
113     for(var i=0; i<this._ants.length; i++)
114     {
115         for(var j=i+1;j<this._ants.length; j++)
116         {
117             if(this._ants[i]._pos == this._ants[j]._pos&&
118                 !this._ants[i].isLeave()&&
119                 !this._ants[j].isLeave()&&
120                 j!=i)
121             {
122                 this._ants[i].whenMeet();
123                 this._ants[j].whenMeet();
124             }
125         }
126     }
127 }
128 
129 function judgeAllLeave()
130 {
131     for(var i=0; i<this._ants.length; i++)
132     {
133         if(!this._ants[i].isLeave())
134             return false;
135     }
136     return true;
137 }
138 
139 function moveAnts()
140 {
141     this._time +=1;
142     this.judgeMeet();
143     for(var i=0; i<this._ants.length; i++)
144     {
145         if(!this._ants[i].isLeave())
146         {
147             this._ants[i].walk();
148         }
149     }
150 }
151 function resetAnts()
152 {
153 /*
154     _pos.push(60);
155     _pos.push(160);
156     _pos.push(220);
157     _pos.push(320);
158     _pos.push(500);
159     this._ants[0]._pos
160     this._ants[1]
161     this._ants[2]
162     this._ants[3]
163     this._ants[4]
164     */
165 }
166 function sticks()
167 {
168     this._width = 600;
169     this._ants = new Array();
170     this._time = 0;
171     
172     this.makeSticks = makeSticks;
173     this.judgeMeet = judgeMeet;
174     this.judgeAllLeave = judgeAllLeave;
175     this.moveAnts = moveAnts;
176     this.resetAnts = resetAnts;
177 }
178 /*----------manager------------*/
179 function initAnts()
180 {
181     var _ant = new ants();
182     this._ants = _ant.makeAnts();
183 }
184 function initSticks()
185 {
186     var _stick = new sticks();
187     this._sticks = _stick.makeSticks(this._ants);
188 }
189 var cal_p = 0;
190 var stick_p = null;
191 var antDivs_p = null;
192 function moveAntDivs()
193 {
194     for(var i=0; i<antDivs_p.length; i++)
195     {
196         antDivs_p[i].style.left =  88+stick_p._ants[i]._pos;
197     }
198 }
199 function doTest(stick)
200 {
201     var _antDivs = new Array();
202     var _antDiv;
203     //alert(stick._ants.length);//must5
204     for(var i=0; i<stick._ants.length; i++)
205     {
206         _antDiv = document.createElement("DIV");
207         _antDiv.style.position = "absolute";
208         _antDiv.style.width = 20;
209         _antDiv.style.height = 20;
210         _antDiv.innerText = i;
211         _antDiv.style.left = 110+stick._ants[i]._pos;
212         _antDiv.style.top = 225;
213         document.body.appendChild(_antDiv);
214         _antDivs.push(_antDiv);
215     }
216     stick_p = stick;
217     antDivs_p = _antDivs;
218     moveAction();
219 }
220 function moveAction()
221 {
222     moveDiv();
223     stick_p.moveAnts();
224     cal_p = 0;
225     if(stick_p.judgeAllLeave())
226     {
227         alert(stick_p._time/10+"S");
228         _manager._results.push(stick_p._time/10);
229         while(antDivs_p.length>0)
230         {
231             var _div = antDivs_p.pop();
232             document.body.removeChild(_div);
233         }
234         window.location.reload();
235     }
236     else
237         setTimeout("moveAction();",1);        
238 }
239 function moveDiv()
240 {
241     for(var i=0; i<stick_p._ants.length; i++)
242     {
243         if(!stick_p._ants[i].isLeave())
244             antDivs_p[i].style.left = 110+stick_p._ants[i]._pos;
245     }
246 }
247 function doSolute(group)
248 {
249     //for(var i=0; i<this._sticks.length; i++)
250     //{
251         this.doTest(this._sticks[group]);
252     //}
253 }
254 function doSort()
255 {
256     var _min = this._results[0];
257     var _max = this._resules[0];
258     for(var i=1; i<this._results.length; i++)
259     {
260         if(_min>this._results[i])
261             _min = this._results[i];
262         if(_max<this._results[i])
263             _max = this._results[i];
264     }
265     alert("最短时间:"+_min+" 最大时间:"+_max);
266 }
267 function manager()
268 {
269     this._ants = new Array();
270     this._sticks = new Array();
271     this._results = new Array();
272     
273     this.initAnts = initAnts;
274     this.initSticks = initSticks;
275     this.doTest = doTest;
276     this.doSolute = doSolute;
277     this.doSort = doSort;
278 }
C#:
1、Ant.cs
 1*最近培训的一个题目:蚂蚁爬竿using System;
 2*最近培训的一个题目:蚂蚁爬竿
 3*最近培训的一个题目:蚂蚁爬竿namespace AntExcise
 4
2、Environment.cs
  1*最近培训的一个题目:蚂蚁爬竿using System;
  2*最近培训的一个题目:蚂蚁爬竿using System.Collections;
  3*最近培训的一个题目:蚂蚁爬竿using System.Windows.Forms;
  4*最近培训的一个题目:蚂蚁爬竿
  5*最近培训的一个题目:蚂蚁爬竿namespace AntExcise
  6
3、Instance.cs
  1*最近培训的一个题目:蚂蚁爬竿using System;
  2*最近培训的一个题目:蚂蚁爬竿using System.Collections;
  3*最近培训的一个题目:蚂蚁爬竿using System.Windows.Forms;
  4*最近培训的一个题目:蚂蚁爬竿
  5*最近培训的一个题目:蚂蚁爬竿namespace AntExcise
  6

4、 Stick.cs

 1*最近培训的一个题目:蚂蚁爬竿using System;
 2*最近培训的一个题目:蚂蚁爬竿using System.Collections;
 3*最近培训的一个题目:蚂蚁爬竿
 4*最近培训的一个题目:蚂蚁爬竿namespace AntExcise
 5

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
  • 2022-01-22
  • 2022-12-23
  • 2021-09-21
  • 2021-10-26
  • 2021-12-26
猜你喜欢
  • 2021-10-03
  • 2021-07-17
  • 2021-12-20
  • 2022-02-24
  • 2021-08-07
  • 2022-12-23
  • 2021-08-23
相关资源
相似解决方案