企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<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 &gt;= 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 &gt;= 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 &gt;= 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 &gt; 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 &lt;iostream&gt; #include &lt;algorithm&gt; void constriangle(int* a, int n) { int ans = 0; //答案 int len, ma, result; //让i&lt;j&lt;k,这样棍子就不会被重复选了 for(int i=0; i&lt;n; i++){ for(int j=i+1; j&lt;n; j++) { for(int k=j+1; k&lt;n; k++) { len = a[i] + a[j] + a[k]; ma = max(a[i], max(a[j], a[k])); int rest = len-ma; if(ma &lt;rest) { ans = max(ans, len); } } } } cout&lt;&lt;ans&lt;&lt;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)