Machineboy空

프로그래머스 : 저주의 숫자 3 본문

Computer/Coding Test

프로그래머스 : 저주의 숫자 3

안녕도라 2023. 12. 30. 15:15

Mine)

- 규칙에 부합하는 숫자 리스트 미리 생성

- n: index값으로 사용

 

한계) 계산 횟수도 많고, n값 커지면 대응하지 못함

 

using System;
using System.Collections.Generic;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        List<int> arr = new List<int>();

        for (int i = 1; i < 200; i++) {
            if (i % 3 != 0 && !i.ToString().Contains("3")) {
                arr.Add(i);
            }
        }

        arr.Sort();

        answer = arr[n-1];

        return answer;
    }
}

 

다른 사람 풀이)

- n: 반복 횟수로 사용

 

using System;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        int count = 1;
        for(int i=0;i<n;i++)
        {
            answer++;
            while(answer%3==0||answer.ToString().Contains("3"))
            {
                answer++;
            }
        }
        return answer;
    }
}