/* 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 = 13; static int c1 = 1; static int a2 = 10; static int b2 = 50; static int c2 = 5; static int a3 = 3; static int b3 = 10; static 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 |