<table border="1" style="font-family:Simsun; border-collapse:collapse; padding:1pt 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:宋體">三角形</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'"><span style="font-size:14pt; font-family:宋體">????有<span style="font-family:Times New Roman">n</span><span style="font-family:宋體">根棍子,棍子</span><span style="font-family:Times New Roman">i</span><span style="font-family:宋體">的長度為</span><span style="font-family:Times New Roman">a</span></span><span style="font-size:14pt; font-family:宋體; vertical-align:sub">i</span><span style="font-size:14pt; font-family:宋體">,想要從中選出三根棍子組成周長盡可能長的三角形。請輸出最大的周長,若無法組成三角形則輸出<span style="font-family:Times New Roman">0</span><span style="font-family:宋體">。</span></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:宋體"><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:宋體">n=5</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:宋體">a={2,3,4,5,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'"><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:宋體">12(<span style="font-family:宋體">選擇</span><span style="font-family:Times New Roman">3,4,5</span><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:宋體">?</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:宋體">n=4</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:宋體">a={4,5,10,20}</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:宋體">????0(<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>
### 【解法一】
### 解題分析:
我的思路(java實現)是:
1.先將棍子根據長度從小到大排序;
2.第一根棍子取最長的a1?=?a[n-1];
3.第二根棍子取次長的a2=a[n-2];
4.第三根棍子取a3=a[n-3],如果a1+a2>a3,則說明可以構成三角形,輸出最大周長;
5.如不能構成三角形,則a3依次取a[n-4],?a[n-5]...
6.如果a3=a[0]時依舊不行,則要循環a2,?使a2依次取a[n-3],a[n-4]...
7.如果再不行,要變換a1值,a1=a[n-2],?a2=a[n-3]...??也就是使a1,a2,a3依次從高到低取值。
8.a1,a2,a3分別取到最小值a[2],a[1],a[0]還是不能構成三角形時,輸出0,結束計算.
書上的分析(用C++實現)是:
直接用三重循環枚舉所有的根子選擇方案。再用以下公式判斷能否構成三角形。
最長棍子的長度<其余兩個棍子的長度之和
### 程序實現:
**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>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>void</strong></span><span style="font-size:10.5pt; font-family:'Courier New'">?contructTriangle(</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'">?a[])?{</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(63,127,95); font-size:10.5pt; font-family:'Courier New'">//?對數組a進行排序</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'">Arrays.</span><span style="font-size:10.5pt; font-family:'Courier New'"><em>sort</em></span><span style="font-size:10.5pt; font-family:'Courier New'">(a);</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'">?n?=?a.</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="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'">?a1,?a2,?a3;</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?=?n?-?1;?i?>=?0;?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'">a1?=?a[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>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'">?j?=?i?-?1;?j?>=?0;?j--)?{</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'">a2?=?a[j];</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="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'">?k?=?j?-?1;?k?>=?0;?k--)?{</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'"/><span style="font-size:10.5pt; font-family:'Courier New'">a3?=?a[k];</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'"/><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'">?(a2?+?a3?>?a1)?{</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'"/><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(</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'">?+?(a1?+?a2?+?a3)?+?</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'">?+?a1?+?</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'">?+?a2?+?</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'"/></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'"/><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'">+?a3?);</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'"/><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="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="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="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(</span><span style="color:rgb(42,0,255); font-size:10.5pt; font-family:'Courier New'">"0,??不能構成三角形"</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><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'"/><pre name="code" class="cpp">#include <iostream>
#include <algorithm>
void constriangle(int* a, int n) {
int ans = 0; //答案
int len, ma, result;
//讓i<j<k,這樣棍子就不會被重復選了
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++) {
for(int k=j+1; k<n; k++) {
len = a[i] + a[j] + a[k];
ma = max(a[i], max(a[j], a[k]));
int rest = len-ma;
if(ma <rest) {
ans = max(ans, len);
}
}
}
}
cout<<ans<<endl;
}
int main() {
int a[5] = {2, 3, 4, 5, 10};
constriangle(a, 5);
return 0;
}</pre><br/><p/></td></tr></tbody></table>
****
### 算法復雜度:
???時間復雜度:O(n3)