<table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>问题主题:</strong></span><span style="font-size:14pt; font-family:宋体">Ants(POJ No.1852)</span><span style="font-size:14pt; font-family:宋体"/></p></td></tr><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border-left-width:0.5pt; border-style:none solid solid; border-left-color:rgb(0,0,0); border-right-width:0.5pt; border-right-color:rgb(0,0,0); border-bottom-width:0.5pt; border-bottom-color:rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>问题描述:</strong></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">n<span style="font-family:宋体">只蚂蚁以每秒</span><span style="font-family:Times New Roman">1cm</span><span style="font-family:宋体">的速度在长为</span><span style="font-family:Times New Roman">Lcm</span><span style="font-family:宋体">的竹竿上爬行。当蚂蚁看到竿子的端点时就会落下来。由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反方向爬行。对于每只蚂蚁,我们只知道它离竿子最左端的距离为</span><span style="font-family:Times New Roman">x</span></span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">i</span><span style="font-size:14pt; font-family:宋体">,但不知道它当前的朝向。请计算所有蚂蚁落下竿子的最短时间和最长时间。</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:21pt"><span style="font-size:10.5pt"><img width="499" height="222" src="image/8be43a614ca632ec28e7e4e33048e613.jpg" alt=""/></span><span style="font-size:14pt; font-family:宋体"> </span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>限制条件:</strong></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">1<=L<=10</span><span style="font-size:14pt; font-family:宋体; vertical-align:super">6</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">1<=n<=10</span><span style="font-size:14pt; font-family:宋体; vertical-align:super">6</span><span style="font-size:14pt; font-family:宋体; vertical-align:super"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">0<=x</span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">i</span><span style="font-size:14pt; font-family:宋体"><=L</span><span style="font-size:14pt; font-family:宋体; vertical-align:super"/></p></td></tr><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border-left-width:0.5pt; border-style:none solid solid; border-left-color:rgb(0,0,0); border-right-width:0.5pt; border-right-color:rgb(0,0,0); border-bottom-width:0.5pt; border-bottom-color:rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>样例:</strong></span><span style="font-size:14pt; font-family:宋体"><strong/></span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">输入</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">L=10</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">n=3</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">x={2,6,7}</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">输出</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">min=4{<span style="font-family:宋体">左、右、右</span><span style="font-family:Times New Roman">}</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">max=8{<span style="font-family:宋体">右、右、右</span><span style="font-family:Times New Roman">}</span></span><span style="font-size:14pt; font-family:宋体"/></p></td></tr></tbody></table>
### 【解法一】
### 解题分析:
对于最短时间,我们可以考虑当所有蚂蚁都向最近的端点移动时,这时不会发生两只蚂蚁相碰的情况,也就是时间最短的情况。
对于最长时间,你也许会想蚂蚁有向左向右两种情况,相碰之后又向相反的方向移动,n只蚂蚁就有2n种可能,要考虑的情况就会特别多,而随n的增大急剧增加。但你仔细想一下两只蚂蚁相遇时的情况(如下图)会发现,由于相遇时相互反向移动且速度相同,我们可以认为是依原方向移动。
如果你是高中生,一定会立马想到物理学中的动能定理……
![](https://box.kancloud.cn/2016-08-24_57bd6c179761d.jpg)
### 程序实现:
**C++**
<table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong><u>#include</u></strong></span><span style="font-size:10.5pt; font-family:'Courier New'"><u> </u></span><span style="color:rgb(42,0,255); font-size:10.5pt; font-family:'Courier New'"><u><iostream></u></span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong><u>#include</u></strong></span><span style="font-size:10.5pt; font-family:'Courier New'"><u> </u></span><span style="color:rgb(42,0,255); font-size:10.5pt; font-family:'Courier New'"><u><algorithm></u></span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"> </span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"> </span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>const</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> L = 10;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>const</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> n = 3;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>const</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> x[n] = {2,6,7};</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="font-size:10.5pt; font-family:'Courier New'"><strong>main</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">() {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> min, max;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">min = max = 0;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> minX, maxX;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>for</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">(</span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">=0; </span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'"><n; </span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">++) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">minX = x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">]<L-x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">]?x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">]:L-x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">];</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">min = minX>=min?minX:min;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">maxX = x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">]>(L-x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">])?x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">]:L-x[</span><span style="font-size:10.5pt; background-color:rgb(192,192,192); font-family:'Courier New'">i</span><span style="font-size:10.5pt; font-family:'Courier New'">];</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">max= maxX>max ? maxX : max;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"><u>cout</u></span><span style="font-size:10.5pt; font-family:'Courier New'"><<min<<</span><span style="color:rgb(42,0,255); font-size:10.5pt; font-family:'Courier New'">" "</span><span style="font-size:10.5pt; font-family:'Courier New'"><<max<<</span><span style="font-size:10.5pt; font-family:'Courier New'"><u>endl</u></span><span style="font-size:10.5pt; font-family:'Courier New'">;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>return</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> 0;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:宋体"/></p></td></tr></tbody></table>
**Java****
<table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>package</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> programdesign;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"> </span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>public</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>class</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> Ants {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>public</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>static</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>void</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> solve(</span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> l, </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> x[]) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>if</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">(0 >= x.</span><span style="color:rgb(0,0,192); font-size:10.5pt; font-family:'Courier New'">length</span><span style="font-size:10.5pt; font-family:'Courier New'">)</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>return</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> min , max;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">min = max = 0;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> d1, d2;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>for</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">(</span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> i=0; i<x.</span><span style="color:rgb(0,0,192); font-size:10.5pt; font-family:'Courier New'">length</span><span style="font-size:10.5pt; font-family:'Courier New'">; i++) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>if</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">(x[i]<=l-x[i]) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">d1 = x[i];</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">d2 = l-x[i];</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">} </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>else</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">d1 = l-x[i];</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">d2 = x[i];</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>if</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">(min < d1) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">min = d1;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>if</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">(max < d2) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">max = d2;</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">System.</span><span style="color:rgb(0,0,192); font-size:10.5pt; font-family:'Courier New'"><em>out</em></span><span style="font-size:10.5pt; font-family:'Courier New'">.println(min + </span><span style="color:rgb(42,0,255); font-size:10.5pt; font-family:'Courier New'">" "</span><span style="font-size:10.5pt; font-family:'Courier New'"> + max);</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>public</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>static</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> </span><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>void</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> main(String[] args) {</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="color:rgb(127,0,85); font-size:10.5pt; font-family:'Courier New'"><strong>int</strong></span><span style="font-size:10.5pt; font-family:'Courier New'"> x[] = {2,6,7};</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'"><em>solve</em></span><span style="font-size:10.5pt; font-family:'Courier New'">(10, x);</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"/><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'"> </span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:'Courier New'">}</span><span style="font-size:10.5pt; font-family:'Courier New'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'Times New Roman'"><span style="font-size:10.5pt; font-family:宋体"><strong> </strong></span></p></td></tr></tbody></table>
****
### 算法复杂度:
时间复杂度O(n)