[Lv.0] 저주의 숫자 3

01수정·2023년 6월 7일
0
post-thumbnail

<입문 100문제> Day 22 - dp, 수학, 조건문, 배열

문제



풀이

(1) 항상 1개 더 많이 나와서 -1 을 굳이 해주었다. 이유를 모르겠어서 2번의 풀이로 변경

function solution(n) {
    let cursed_nums = [];
    
    let counter = 1;
    do {
        if (counter%3 === 0 || counter.toString().includes(3)) {
            cursed_nums.push(counter);
            n++;
        } 
        counter++;
    } while(counter <= n) 
        
    return counter-1;
}

(2)

function solution(n) {
    let not_cursed = [];
    let num = 0;
    
    while(not_cursed.length !== n && ++num) {
        if (num%3 !== 0 && !num.toString().includes('3')) {
            not_cursed.push(num);
        } 
    } 

    console.log(n, not_cursed.length, not_cursed);
    return not_cursed.pop();
}

해답

profile
새싹 FE 개발자

0개의 댓글