[백준|node.js] 1225번, 이상한 곱셈 (for문) - 수학, 구현, 문자열

muz·2022년 7월 14일
0
post-thumbnail

✍🏻 문제 확인하기

접근방법

  • 한 자리씩 뽑아 곱하는 가능한 모든 조합의 수를 구하려면, for문을 중첩해 각 자릿수를 곱해주면 됨

풀이

const input = require('fs').readFileSync('/dev/stdin').toString().split(' ').map(el => el.split(''));

const a = input[0];
const b = input[1];
let sum = 0;
for(let i = 0; i < a.length; i++) {
    for(let j = 0; j < b.length; j++) {
        sum += a[i]*b[j];
    }
}

console.log(sum);

해설

  • 바깥 for문은 a의 길이만큼, 안쪽 for문은 b의 길이만큼 반복하여 각각의 자릿수를 곱해 sum에 누적합한다.
profile
Life is what i make up it 💨

0개의 댓글