728x90

https://www.acmicpc.net/problem/1065

Code

최종 제출 코드 https://github.com/DasolPark/Algorithm_JavaScript/blob/b9164f18301aec45680c44eeb204a6500f53bcdd/Baekjoon/1065.js
기존 제출 코드https://github.com/DasolPark/Algorithm_JavaScript/commit/95a24f7f1c0887af6d8a7e1a55e8533f0850150e

 

😢 '어떤 양의 정수 X의 자리수가 등차수열을 이룬다면'???? 지금 보면 별 것 아니지만, 처음 문제를 볼 때는 비슷한 문제를 풀어보지 못해서 그런지 이해하기 힘들었다. 문제 이해하는 것에 시간을 많이 보냈다.

😊 즉, 100미만의 수두 자릿수 또는 한 자릿수여서 등차수열을 확인할 수 없기 때문에 모두 '한수'가 된다.
그리고 세 자릿수부터 연속된 등차수열을 확인할 수 있다.
예를 들어, 우리에게 주어진 숫자 111을 백의 자리 1, 십의 자리1, 일의 자리1 하나씩 분리했다고 치자.
백의자리 1 - 십의자리 1 = 0 그리고 십의자리 1 - 일의자리 1 = 0 ( 1-1=0 === 1-1=0, true )
위처럼 연속된 두 개의 수 차이가 0으로 같다. 그렇다면 이건 한수이다.
그리고 1000은 '한수'가 아니다.

Solution 1) 기존 제출 코드
for loop안에서 100미만은 모두 한수로 count,
1000미만은 각 자릿수를 분리하여 Array에 넣고 각 자릿수 순서대로 빼주어 등차수열을 확인하였다.
만약 그 값이 한수라면 한수를 count해주었다. 그리고 1000은 한수가 아니기 때문에 else{ break; }으로 작성해주었다.

Solution 2) Other Solution
이 해결 방법은 다른 정답자의 답안을 참고하였다. 다른 방식으로 접근하였기 때문에 꼭 학습해두고 싶었다.
이 해결 방법에서는, classic for loop을 이용해서 function을 반복 호출하고 true값을 이용해 한수를 count해주었다.
특히, 세 자릿수가 들어오면 toString().split('').map을 이용해 Array로 만들어주고 값을 빼주어서 등차를 확인하였다.

Solution 3) 최종 제출 코드
Solution 2)의 개선 버전이다. for loop을 2번 사용하지 않고, function 안에서 한 번만 for loop을 사용했다.

자릿수를 다루는 문제와 직면할 때는, while을 이용하여 자릿수를 분리하거나
 문자열의 index를 이용하여 해결하곤 하는데, 항상 두 가지를 다 이용해보곤 한다.
프로그래밍은 한 가지 방법만 있지 않다는 게 가장 큰 매력인 것 같다.

Full Code

// Hansoo(category - function)
 
// 1st Solution
 
// For submit
 
// const fs = require('fs');
// const N = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
 
function hansoo(N) {
const numArr = [];
let hansoo, digitCount;
let temp = 0;
 
for (let i = 1; i <= N; i++) {
if (i < 100) {
hansoo = i;
} else if (i < 1000) {
digitCount = 0;
temp = i;
while (temp > 0) {
numArr[digitCount] = temp % 10;
temp = parseInt(temp / 10);
digitCount++;
}
if (numArr[0] - numArr[1] === numArr[1] - numArr[2]) {
hansoo++;
}
} else {
break;
}
}
 
return hansoo;
}
 
// For Test
console.log(hansoo(110));
console.log(hansoo(1));
console.log(hansoo(210));
console.log(hansoo(1000));
 
// For submit
// console.log(hansoo(N));
 
// Other Solution(Solution 2)
 
// For submit
// const fs = require('fs');
// const N = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
 
// For Test
// const N = 110;
 
// function findHansoo(N) {
// if (N < 100) {
// return true;
// } else {
// const numArr = N.toString()
// .split('')
// .map(num => {
// return parseInt(num);
// });
// if (numArr[0] - numArr[1] === numArr[1] - numArr[2]) {
// return true;
// }
// }
 
// return false;
// }
 
// let hansoo = 0;
// for (let i = 1; i <= N; i++) {
// if (findHansoo(i)) {
// hansoo++;
// }
// }
 
// console.log(hansoo);
 
// 2nd Solution
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim();
 
// For local test
// const input = '1000';
 
// function hanCheck(num) {
// num = parseInt(num);
// let counter = 0;
// for (let i = 1; i <= num; i++) {
// if (i < 100) {
// counter++;
// } else if (i < 1000) {
// let temp = i;
// let numArr = [];
// let index = 0;
// while (temp > 0) {
// numArr[index] = temp % 10;
// temp = parseInt(temp / 10);
// index++;
// }
// if (numArr[0] - numArr[1] === numArr[1] - numArr[2]) {
// counter++;
// }
// }
// }
// console.log(counter);
// }
 
// hanCheck(input);
 
// 3rd Solution(최종 제출 코드)
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim();
 
// For local test
// const input = '110';
// const N = parseInt(input);
 
// function hanCheck(N) {
// let hansoo = 0;
// for (let i = 1; i <= N; i++) {
// if (i < 100) {
// hansoo++;
// } else {
// const numArr = i
// .toString()
// .split('')
// .map(num => parseInt(num));
// if (numArr[0] - numArr[1] === numArr[1] - numArr[2]) {
// hansoo++;
// }
// }
// }
// console.log(hansoo);
// }
 
// hanCheck(N);

+ Recent posts