

자리수를 비교하는 것이 포인트인 문제이다.
문제 해결의 단계는 아래와 같다.
L로 최솟값, R로 최댓값을 받고, 각 자리의 숫자를 비교하기 위해 string으로 변환한다.counter 변수를 통해 8의 최소 개수를 셀 것이다. 0으로 초기화한다.L과 R의 자리수가 다르면 0을 출력한다.L과 R의 각 자리의 숫자가 같으면서 그 숫자가 8이면 counter를 하나 증가한다. L과 R의 각 자리의 숫자가 다를경우 반복문을 종료한다.counter 값을 출력한다.const input = require("fs")
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
.toString()
.trim()
.split(/\s+/) // \s: 공백 문자, + : 하나 이상 연속 된 경우
.map(Number);
const L = input[0].toString();
const R = input[1].toString();
let counter = 0;
if(L.length != R.length){
console.log(counter);
}else{
for(let i=0; i<L.length; i++){
if(L[i] === R[i] && L[i] == 8){
counter++;
}else if(L[i] != R[i]){
break;
}
}
console.log(counter);
}
const input = require("fs")
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
.toString()
.trim()
.split(/\s+/) // \s: 공백 문자, + : 하나 이상 연속 된 경우
.map(Number);
const L = input[0].toString();
const R = input[1].toString();
let counter = 0;
if(L.length != R.length){
console.log(counter);
}else{
for(let i=0; i<L.length; i++){
if(L[i] != R[i])
break;
counter++;
}
console.log(counter);
}
처음에는 단지 자릿수 비교, 각 자리의 숫자 비교해서 같거나 다를 경우에 대해서만 코드를 작성했다. 이 경우 180 189와 같은 입력값이 들어왔을 때는 2를 출력한다. 그 이유는 8과의 비교를 놓쳤기 때문이다.
💡한줄평
일주일 간 일본 여행을 끝내고 오랜만에 돌아와서 문제풀이를 진행중이다. 다시 열심히 공부할 것이다.