[1110] 더하기 사이클 _ node.js

박서현·2023년 8월 4일
0
post-thumbnail

🍏1110_더하기 사이클

🔻나의 풀이

// let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let input = require('fs').readFileSync('txt/1110.txt').toString().split('\n');
let number = +input[0];


//십의자리수 : a, 일의자리수 : b
function separateTwoDigits(newNumber, count) {
    let a = newNumber < 10 ? 0 : Math.floor(newNumber / 10);
    let b = newNumber % 10;

    let sum = a + b;
    count++

    let nextNumber = b * 10 + sum % 10;

    return nextNumber !== number ? separateTwoDigits(nextNumber, count) : count;
}


//메인함수
function main() {
    let count = 0;
    console.log(separateTwoDigits(number, count));
}

main();

🔻호출 부분이 간단할수록 좋다
``javascript
// const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const input = require('fs').readFileSync('txt/1110.txt').toString().split('\n');
const num = +input[0];

//십의자리수 : tens, 일의자리수 : ones
function splitDigits(num, newNumber = num, count = 0) {
const tens = Math.floor(newNumber / 10);
const ones = newNumber % 10;

const sum = tens + ones;
count++

const nextNumber = ones * 10 + sum % 10;

return nextNumber !== num ? splitDigits(num, nextNumber, count) : count;

}

//메인함수
function main() {
console.log(splitDigits(num));
}

main();

<img style="width:450px;" src="https://velog.velcdn.com/images/p_seo_hn/post/2b2d8af7-3d83-4bb9-9fe6-3d6c79651861/image.png">



# Programmers
### [🍏순서쌍의 개수](https://school.programmers.co.kr/learn/courses/30/lessons/120836)

![](https://velog.velcdn.com/images/p_seo_hn/post/3bcf3e85-b393-4525-9987-63dfffe087be/image.png)

🔻나의 풀이
```javascript
function solution(n) {
    let count = 0;
    for (let i = 1; i <= n; i++){
        if ( n % i === 0){
        	let j = n % i;
        	count++
        }
    }
    return count;
}

0개의 댓글