본문 바로가기

프로그래머스/lv0

저주의 숫자 3

/* 저주의 숫자 3
 * 3x 마을 사람들은 3을 저주의 숫자라고 생각하기 때문에
 * 3의 배수와 숫자 3을 사용하지 않습니다.
 * 3x 마을 사람들의 숫자는 다음과 같습니다.
 *
 * 10진법 3x 마을에서 쓰는 숫자
 * 1        1                      
 * 2        2                    
 * 3        4   -
 * 4        5                
 * 5        7   -                
 * 6        8
 * 7        10  -
 * 8        11
 * 9        14  -
 * 10       16  -
 * 정수 n이 매개변수로 주어질 때, n을 3x 마을에서 사용하는 숫자로 바꿔 return
 *
 * n    result
 * 15   25
 * 40   76
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class programmer_0_91 {
    static int a1 = 15;
    static int a2 = 40;
    public int solution(int n) {
        int answer = 0;
        for(int i = 1; i <= n; i++){
            answer++;
            // 3이 들어가지 않으며, 3의 배수 또한 아닌 경우까지 반복
            while(Integer.toString(answer).contains("3"|| answer % 3 == 0) {
                answer++;
            }
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_0_91 t = new programmer_0_91();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        // System.out.println("result2 = " + t.solution(a2));
        // System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

평행  (0) 2022.12.09
안전지대  (1) 2022.12.07
특이한 정렬  (2) 2022.12.07
유한소수 판별하기  (0) 2022.12.07
직사각형의 넓이  (1) 2022.12.07