알고리즘/백준

[boj 4153] 직각삼각형 - js

jinux127 2022. 3. 1. 11:39

문제

과거 이집트인들은 각 변들의 길이가 3, 4, 5인 삼각형이 직각 삼각형인것을 알아냈다. 주어진 세변의 길이로 삼각형이 직각인지 아닌지 구분하시오.

입력

입력은 여러개의 테스트케이스로 주어지며 마지막줄에는 0 0 0이 입력된다. 각 테스트케이스는 모두 30,000보다 작은 양의 정수로 주어지며, 각 입력은 변의 길이를 의미한다.

출력

각 입력에 대해 직각 삼각형이 맞다면 "right", 아니라면 "wrong"을 출력한다.

// 직각삼각형

// const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n'); // 제출용
const input = require('fs').readFileSync('input.txt').toString().trim().split('\n'); // vscode 테스트용

const arr = input.map(item=>item.split(' ').map(Number));

const sol = (arr) =>{
    for(const item of arr){
        item.sort((a,b)=>a-b);
        if(item[0] === 0) break;
        const x = Math.pow(item[0],2);
        const y = Math.pow(item[1],2);
        const z = Math.pow(item[2],2);
        x + y === z ? console.log('right') : console.log('wrong');
    }
}

sol(arr);

 

직각삼각형의 공식인 변이 a,b,c가 주어지고 c가 가장 큰 변의 길이일때 a^2 + b^2 = c^2 을 사용하면 된다