미누에요

[백준 7568번] 덩치 (JavaScript) 본문

알고리즘

[백준 7568번] 덩치 (JavaScript)

미누라니까요 2025. 1. 8. 23:58
728x90
반응형
SMALL

 

 

몸무게와 키를 입력받아 비교하여 순위를 매기는 문제이다.

 

나는 우선 Person이라는 클래스를 만들어 키, 몸무게, 등수를 내부변수로 두었다.

비교는 이중 for문을 사용하여 각각 비교할 수 있도록 구성하였다.

 

소스 코드

const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');

const num = Number(input[0]);
const values = input.slice(1);
const data = values.map((ele) => ele.split(' ').map(Number));

class Person {
    constructor(weight, height) {
        this.weight = weight;
        this.height = height;
        this.rank = 1;
    }
}

const personInfo = data.map(([weight, height]) => new Person(weight, height));

for (let i = 0; i < num; i++) {
    for (let j = 0; j < num; j++) {
        if (i !== j) {
            if (
                personInfo[i].weight < personInfo[j].weight &&
                personInfo[i].height < personInfo[j].height
            ) {
                personInfo[i].rank++;
            }
        }
    }
}

console.log(personInfo.map((person) => person.rank).join(' '));
728x90
반응형
LIST