Archives
Recent Posts
«   2025/07   »
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
11번가
Today
Total
07-12 02:59
관리 메뉴

smwhee

[코딩도장_문제7] 1부터 10,000까지 8이라는 숫자가 총 몇번 나오는가? 본문

Development

[코딩도장_문제7] 1부터 10,000까지 8이라는 숫자가 총 몇번 나오는가?

smwhee 2017. 9. 16. 00:00

package codingDojang;


/*

 * 구글 입사문제 중에서

 * 

 * 1부터 10,000까지 8이라는 숫자가 총 몇번 나오는가?

 * 8이 포함되어 있는 숫자의 갯수를 카운팅 하는 것이 아니라 8이라는 숫자를 모두 카운팅 해야 한다.

 * (※ 예를들어 8808은 3, 8888은 4로 카운팅 해야 함)

 */


public class Question_007 {

public static void main(String[] args) {

int cnt = 0;

for (int i = 1; i <= 10000; i++) {

char[] c = Integer.toString(i).toCharArray();

int len = c.length;

for (int j = 0; j < len; j++) {

if (c[j] == '8') {

cnt++;

}

}

}

System.out.println(cnt);

}

}



Comments