본문 바로가기

프로그래머스/lv0

k의 개수

/* k의 개수
 * 1부터 13까지의 수에서,
 * 1은 1, 10, 11, 12, 13 이렇게 총 6번 등장합니다.
 * 정수 i, j, k가 매개변수로 주어질 때,
 * i부터 j까지 k가 몇 번 등장하는지 return
 *
 * i    j   k   result
 * 1    13  1   6
 * 10   50  5   5
 * 3    10  2   0
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class programmer_0_31 {
    static int a1 = 1;  static int b1 = 13static int c1 = 1;
    static int a2 = 10static int b2 = 50static int c2 = 5;
    static int a3 = 3;  static int b3 = 10static int c3 = 2;
    public int solution(int i, int j, int k) {
        //다른사람 풀이법 신기하네.
        String str = "";
        for(int a = i; a <= j; a++) {
            str += a+"";
            System.out.println(str);
        }
        System.out.println("-------");
        System.out.println(str);
        System.out.println(str.replace(k+""""));
        System.out.println(str.length() + " / " + str.replace(k+"""").length());
 
        return str.length() - str.replace(k+"""").length();
/*
        int answer = 0;
        for(int x = i; x <= j; x++){
            String target = Integer.toString(x); 
            if(target.contains(Integer.toString(k))){
                for(int y = 0; y < target.length(); y++){
                    if(target.substring(y,y+1).contains(Integer.toString(k))){
                        answer++;
                    } 
                }
            }   
        }
        return answer;
 */        
    }
    public static void main(String args[]){
        programmer_0_31 t = new programmer_0_31();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1,b1,c1));
        System.out.println("---------------------------------------");
        // System.out.println("result2 = " + t.solution(a2,b2,c2));
        // System.out.println("---------------------------------------");
        // System.out.println("result3 = " + t.solution(a3,b3,c3));
        // System.out.println("---------------------------------------");
    }
}
cs

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

분수의 덧셈  (1) 2022.12.05
중복된 문자 제거  (0) 2022.12.05
A로 B 만들기  (0) 2022.12.05
최빈값 구하기  (1) 2022.12.05
두 수의 나눗셈  (0) 2022.12.05