코딩테스트 뿌수기

[백준 7568번 node.js] 덩치

hans-j 2024. 8. 12. 23:23

https://www.acmicpc.net/problem/7568

 

학생 N명의 몸무게와 키가 담긴 입력을 읽어서 각 사람의 덩치 등수를 계산하여 출력


여담

 

코딩테스트를 꽤나 간만에 풀었더니... 매일 풀 때와 달리 시작 전부터 버벅 거렸다.

 

낯설다 니네..

 

이럴때는

어떻게 처리해야할지 먼저 단계적으로 적어보고 

주어진 것, 요구하는 것, 필요한 로직 이렇게 나누어서 적어보면

어떻게 접근해야하는지 시동이 걸리는 느낌이다.

다시 코테와 친근해지기전까지는 계속 이럴 듯..

 


 

해결방법

 

1. 주어진 문자열에서 필요한 값 추출

const inputLines = input.trim().split("\n");
const peopleCount = Number(inputLines[0]); // 사람 수
const people = inputLines.slice(1).map((row) => row.split(" ").map(Number));

 

2.  덩치 수를 저장할 배열

const bodysize = [];

 

3. 덩치를 비교하는 로직 생성

// 각 사람의 덩치 수를 계산
    for (let i = 0; i < peopleCount; i++) {
        let currentPerson = people[i];
        let count = 1; // 자기 자신을 포함해서 기본값은 1

        for (let j = 0; j < peopleCount; j++) {
            if (i !== j) {
                // 자기 자신과 비교하지 않기
                let otherPerson = people[j];
                // 다른 사람이 현재 사람보다 덩치가 더 큰 경우
                if (
                    otherPerson[0] > currentPerson[0] &&
                    otherPerson[1] > currentPerson[1]
                ) {
                    count++; // 덩치 수 증가
                }
            }
        }

        // 결과 배열에 추가
        bodysize.push(count);
    }

 

 

4.  완성

const inputvalue = `5
55 185
58 183
88 186
60 175
46 155`;

const calculateBodysize = (input) => {
    // 입력 값을 처리
    const inputLines = input.trim().split("\n");
    const peopleCount = Number(inputLines[0]); // 사람 수
    const people = inputLines.slice(1).map((row) => row.split(" ").map(Number));

    // 덩치 수를 저장할 배열
    const bodysize = [];

    // 각 사람의 덩치 수를 계산
    for (let i = 0; i < peopleCount; i++) {
        let currentPerson = people[i];
        let count = 1; // 자기 자신을 포함해서 기본값은 1

        for (let j = 0; j < peopleCount; j++) {
            if (i !== j) {
                // 자기 자신과 비교하지 않기
                let otherPerson = people[j];
                // 다른 사람이 현재 사람보다 덩치가 더 큰 경우
                if (
                    otherPerson[0] > currentPerson[0] &&
                    otherPerson[1] > currentPerson[1]
                ) {
                    count++; // 덩치 수 증가
                }
            }
        }
        
        // 결과 배열에 추가
        bodysize.push(count);
    }

    return bodysize;
};


// 테스트 입력
console.log(calculateBodysize(inputvalue).join(" "));

 

5. 제출

const inputvalue = require("fs").readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt").toString().trim();

const calculateBodysize = (input) => {
    // 입력 값을 처리
    const inputLines = input.trim().split("\n");
    const peopleCount = Number(inputLines[0]); // 사람 수
    const people = inputLines.slice(1).map((row) => row.split(" ").map(Number));

    // 덩치 수를 저장할 배열
    const bodysize = [];

    // 각 사람의 덩치 수를 계산
    for (let i = 0; i < peopleCount; i++) {
        let currentPerson = people[i];
        let count = 1; // 자기 자신을 포함해서 기본값은 1입니다

        for (let j = 0; j < peopleCount; j++) {
            if (i !== j) {
                // 자기 자신과 비교하지 않기
                let otherPerson = people[j];
                // 다른 사람이 현재 사람보다 덩치가 더 큰 경우
                if (
                    otherPerson[0] > currentPerson[0] &&
                    otherPerson[1] > currentPerson[1]
                ) {
                    count++; // 덩치 수 증가
                }
            }
        }

        // 결과 배열에 추가
        bodysize.push(count);
    }

    return bodysize;
};

console.log(calculateBodysize(inputvalue).join(" "));

 

 


이번 문제의 키포인트는 배열끼리의 값 비교하기!! 이거는 초급 난이도의 코테에서 정~~~말 많이 나오는 유형같다..

 

다른분들이 푼 풀이를 보니까 정말 간결하던데 ... 갈 길이 멀다 열심히 해야지