1process.stdin.resume(); 2process.stdin.setEncoding('utf-8'); 3let input = ''; 4 5process.stdin.on('data', (data) => { 6 input += data; 7}); 8 9process.stdin.on('end', () => { 10 let inputArray = input.trim().split(' ').map(Number); // 转换为整数数组并去除首尾空白 11 12 function doFunc() { 13 let maxPerimeter = 0; 14 const n = inputArray.length; 15 inputArray.sort((a, b) => b - a); // 从大到小排序 16 17 for (let i = 0; i < n - 2; i++) { 18 for (let j = i + 1; j < n - 1; j++) { 19 for (let k = j + 1; k < n; k++) { 20 if (inputArray[i] < inputArray[j] + inputArray[k] && 21 inputArray[j] < inputArray[i] + inputArray[k] && 22 inputArray[k] < inputArray[i] + inputArray[j]) { 23 // 可以组成三角形,计算周长并更新最大值 24 const perimeter = inputArray[i] + inputArray[j] + inputArray[k]; 25 if (perimeter > maxPerimeter) { 26 maxPerimeter = perimeter; 27 } 28 } 29 } 30 } 31 } 32 33 console.log(maxPerimeter); // 输出最大周长或0(如果没有找到三角形) 34 } 35 36 doFunc(); 37 process.exit(); 38});
Copyright ©2010-2022 比特日记 All Rights Reserved.
Powered By 可尔物语