<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的增大急劇增加。但你仔細想一下兩只螞蟻相遇時的情況(如下圖)會發現,由于相遇時相互反向移動且速度相同,我們可以認為是依原方向移動。
如果你是高中生,一定會立馬想到物理學中的動能定理……

### 程序實現:
**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)