๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[๋ฐฑ์ค 1110] ๋ํ๊ธฐ ์ฌ์ดํด (Javascript)
โ๏ธIdea Sketch
2021-07-19
1. ๋ฌธ์ ์ดํด
- 26, 2+6 = 8, 68, 6+8 = 14, 84, 8+4 = 12, 42, 4+2 = 6, 26
2. ์ญ์ ์๋ฆฌ ์์ ์ผ์ ์๋ฆฌ ์ ๋ํ๊ธฐ
let num = 26;
let sum = parseInt(num%10 + num/10);
console.log(sum);
3. ๋ํ๊ธฐ ์ ์๋ฅผ ์ ์ฅํด๋์ผ ํจ
4. ๋ํ๊ธฐ ์ ์ผ์ ์๋ฆฌ์ (์ญ์ ์๋ฆฌ๋ก ๋ง๋ค์ด์ผ ํจ), ๋ํ ํ ์ผ์ ์๋ฆฌ์ ํฉ์น๊ธฐ
num = num%10 + sum%10;
5. ์นด์ดํธ ์ธ๊ธฐ, ๋ฐ๋ณต๋ฌธ
- ์๋ก์ด ์๊ฐ ์๊ธธ ๋๋ง๋ค cnt++;
- ์๋ก์ด ์๊ฐ ์๊ธธ ๋๋ง๋ค ์๋ ์์ ์ผ์นํ๋์ง ํ์ธ
let num = N;
let cnt = 0;
while(num !== N) {
let sum = parseInt(num%10 + num/10);
num = num%10 + sum%10;
cnt++;
}
return cnt;
โ๏ธ์์ค์ฝ๋
2021-07-19 ํต๊ณผ, 168ms
let fs = require('fs');
let input = fs.readFileSync('../stdin').toString().split('\n');
let N = parseInt(input[0]);
function solution(N) {
let cnt = 0;
let num = N;
while (true) {
let sum = parseInt(num%10 + num/10);
num = (num%10)*10 + sum%10;
cnt++;
if (num === N) break;
}
return cnt;
}
console.log(solution(N))
2021-07-19 ํต๊ณผ, 136ms
- ํจ์๋ฅผ ์ ์ํ์ง ์์์ ๋ ์๋๊ฐ ๋ ๋น ๋ฆ
let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
let N = parseInt(input[0]);
let cnt = 0;
let num = N;
while (true) {
let sum = parseInt(num%10 + num/10);
num = (num%10)*10 + sum%10;
cnt++;
if (num === N) break;
}
console.log(cnt);