inblog logo
|
silver
    알고리즘문제풀기

    [알고리즘문제풀기] 저주의 숫자 3

    silver's avatar
    silver
    Jan 31, 2025
    [알고리즘문제풀기] 저주의 숫자 3
    Contents
    문제내가 작성한 정답다른 사람들의 정답

    문제

    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/120871

    내가 작성한 정답

    class Solution { public int solution(int n) { int[] village = new int[100]; int num = 0; int count = 0; while (count < n) { num++; if (num % 3 == 0 || String.valueOf(num).contains("3")) { continue; } village[count + 1] = num; count++; } return village[n]; } }

    다른 사람들의 정답

    class Solution { public int solution(int n) { int answer = 0; // 3x 마을에서 사용하는 숫자를 카운트할 변수 int count = 0; // 유효한 3x 마을 숫자의 개수 while (count < n) { // n개의 유효한 숫자가 나올 때까지 반복 answer++; // 숫자를 증가 // answer가 3의 배수이거나 '3'을 포함하는 경우 if (answer % 3 != 0 && !String.valueOf(answer).contains("3")) { count++; // 유효한 숫자일 경우 카운트 증가 } } return answer; // n번째 3x 마을 숫자 반환 } }
    class Solution { public int solution(int n) { String str; // 숫자를 문자열로 변환하기 위한 변수 // 1부터 n까지 반복 for (int i = 1; i <= n; i++) { str = "" + i; // 현재 숫자를 문자열로 변환 // 현재 숫자가 3의 배수이거나 '3'을 포함하는 경우 if (str.contains("3") || i % 3 == 0) { n++; // 유효하지 않은 숫자이므로 n을 증가 } } return n; // 최종적으로 n을 반환 } }
    Share article

    silver

    RSS·Powered by Inblog