memostack
article thumbnail
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.tistory.com/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
반응형
문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다

문제

 

풀이

입력받은 숫자의 곱을 구하고, 각 숫자가 몇개씩 들어갔는지 출력하는 문제이다.

 

대부분 곱을 구한뒤에 문자열로 치환해서 이터레이션 돌면서 값을 구할거 같다는 생각이 들어서(아님 말고), 본 글에서는 나눗셈과 나머지 연산을 사용하여 문제를 풀었다.

const solution = (input) => {
    let rs = input.reduce((acc, s) => acc * +s, 1);
    const numTable = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    while (rs > 0) {
        numTable[rs % 10]++;
        rs = parseInt(rs / 10);
    }
    return numTable.join('\n');
};

const input = [];
require('readline')
    .createInterface({ input: process.stdin })
    .on('line', (line) => input.push(line))
    .on('close', (_) => {
        console.log(solution(input));
        process.exit(0);
    });

 

전체 코드

https://github.com/bluemiv/Algorithm/blob/master/baekjoon/nodejs/src/ex02/ex2577.js

 

GitHub - bluemiv/Algorithm: 알고리즘 풀이

알고리즘 풀이. Contribute to bluemiv/Algorithm development by creating an account on GitHub.

github.com

 

풀이 결과

문제 풀이 결과

 

관련 글

2022.07.21 - [Algorithm/Beakjoon] - jest 단위테스트를 이용하여 백준 알고리즘 문제 편하게 풀기

 

 

반응형
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.tistory.com/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
profile

memostack

@bluemiv_mm

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!