-
[백준 단계별로 풀어보기] 6. 함수 (feat. NodeJS)Development/Program Solving 2020. 12. 27. 22:56728x90
4673번: 셀프 넘버
CASE 1
시간: 108.917ms
const selfNumber = []; const main = () => { for (let i=1; i<=10000; i++) { selfNumber.push(i); }; for(let i=1; i<=10000; i++) { const result = i + i.toString().split("").reduce((pre, cur) => pre + Number(cur),0); const sameIndex = selfNumber.indexOf(result); if(~sameIndex) { selfNumber.splice(sameIndex, 1); } }; for (const number of selfNumber) { console.log(number); }; }; main();
CASE 2
아무래도 splice로 삭제하면 배열 인덱스가 전체적으로 조정되어야하므로, 이 부분을 변경해서 속도 개선을 했다.
시간: 60.878ms
const selfNumber = []; const main = () => { for (let i=1; i<=10000; i++) { selfNumber.push(i); }; for(let i=1; i<=10000; i++) { const result = i + i.toString().split("").reduce((pre, cur) => pre + Number(cur),0); const sameIndex = selfNumber.indexOf(result); if(sameIndex) { selfNumber[sameIndex] = false; } }; for (const number of selfNumber) { if(number) {console.log(number)}; }; }; main();
1065번: 한수
CASE 1
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let number; const onInput = (input) => number = Number(input); const onClose = () => { let firstNum = 0; let answer = []; for (let i = 1; i <= number; i++) { if(i.toString().length >= 3){ const numbers = i.toString().split(""); for (let x = 0; x < numbers.length - 1; x++) { const secondNum = Number(numbers[x]) - Number(numbers[x+1]); if(x !== 0 && firstNum !== secondNum) { break; } else if(x !== 0 && firstNum === secondNum) { answer.push(i); } firstNum = secondNum; }; }else { answer.push(i); } }; console.log(answer.length); process.exit(); }; rl.on('line', onInput) .on('close', onClose);
728x90'Development > Program Solving' 카테고리의 다른 글
[백준 1712번] 손익분기점 (feat. NodeJS) (0) 2021.01.06 [백준 단계별로 풀어보기] 7. 문자열 (feat. NodeJS) (0) 2020.12.29 [백준 단계별로 풀어보기] 3. for문 (feat.NodeJS) (0) 2020.12.25 [백준 단계별로 풀어보기] 5. 1차원 배열 (feat.NodeJS) (0) 2020.12.25 [백준 단계별로 풀어보기] 2. if문 (feat.NodeJS) (0) 2020.12.23