最大三角形面积

最大三角形面积

 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 const pointsStr = input.trim(); 11 const pointsArray = pointsStr.split(',').map(pointStr => { 12 const [x, y] = pointStr.trim().split(' ').map(Number); 13 return [x, y]; 14 }); 15 16 function triangleArea(p1, p2, p3) { 17 const [x1, y1] = p1; 18 const [x2, y2] = p2; 19 const [x3, y3] = p3; 20 return Math.abs(0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))); 21 } 22 23 let maxArea = 0; 24 const origin = [0, 0]; 25 26 for (let i = 0; i < pointsArray.length; i++) { 27 if (pointsArray[i][0] === 0 && pointsArray[i][1] === 0) continue; // Skip the origin itself 28 for (let j = i + 1; j < pointsArray.length; j++) { 29 if (pointsArray[j][0] === 0 && pointsArray[j][1] === 0) continue; // Skip the origin itself 30 const area = triangleArea(origin, pointsArray[i], pointsArray[j]); 31 if (area > maxArea) { 32 maxArea = area; 33 } 34 } 35 } 36 37 console.log(maxArea.toFixed(2)); 38 process.exit(); 39});
process.stdin.resume(); process.stdin.setEncoding('utf-8'); let input = ''; process.stdin.on('data', (data) => { input += data; }); process.stdin.on('end', () => { const pointsStr = input.trim(); const pointsArray = pointsStr.split(',').map(pointStr => { const [x, y] = pointStr.trim().split(' ').map(Number); return [x, y]; }); function triangleArea(p1, p2, p3) { const [x1, y1] = p1; const [x2, y2] = p2; const [x3, y3] = p3; return Math.abs(0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))); } let maxArea = 0; const origin = [0, 0]; for (let i = 0; i < pointsArray.length; i++) { if (pointsArray[i][0] === 0 && pointsArray[i][1] === 0) continue; // Skip the origin itself for (let j = i + 1; j < pointsArray.length; j++) { if (pointsArray[j][0] === 0 && pointsArray[j][1] === 0) continue; // Skip the origin itself const area = triangleArea(origin, pointsArray[i], pointsArray[j]); if (area > maxArea) { maxArea = area; } } } console.log(maxArea.toFixed(2)); process.exit(); });

Powered By 可尔物语

浙ICP备11005866号-12