본문으로 바로가기

온코더 - 13구하기

category 코딩테스트 2018. 12. 21. 02:37
<문제 설명>

정수 int a가 주어집니다.
1이상 int a이하의 정수 중 숫자에 13이 들어가는 숫자의 개수를 리턴하세요.

<참고 / 제약 사항>

a는 1이상, 10,000이하 입니다.



테스트 케이스

(1)
int a = 13
리턴(정답): 1
1이상 13이하의 정수중 13이 들어가는 숫자는 13이 유일 합니다.
(2)
int a = 200
리턴(정답): 12

<내가 제출한 코드>
public static int solution(int a){
int quotient = 0, remainder = 0, result = 0; /* 몫, 나머지, 13의 개수를 저장하기 위한 변수 */
int tmpRemainder; /* 이전 자릿수의 값을 저장하기 위한 변수 */
for(int i=1; i<=a; i++) {
quotient = i;
tmpRemainder = 0;
while(quotient > 0) {
remainder = quotient%10;
quotient = quotient/10;
if (remainder == 1 && tmpRemainder == 3) { /* 현재 자릿수의 값이 1 이고, 이전 자릿수의 값이 3이면 참 */
result++;
break;
}
tmpRemainder = remainder; /* 이전 자릿수의 값 저장 */
}
}
return result;
}