# [Algorithm/JS] ๋ฐฑ์ค€ 2231๋ฒˆ ๋ถ„ํ•ดํ•ฉ

๐Ÿ”— ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ (opens new window)

# Question

์–ด๋–ค ์ž์—ฐ์ˆ˜ N์ด ์žˆ์„ ๋•Œ, ๊ทธ ์ž์—ฐ์ˆ˜ N์˜ ๋ถ„ํ•ดํ•ฉ์€ N๊ณผ N์„ ์ด๋ฃจ๋Š” ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์–ด๋–ค ์ž์—ฐ์ˆ˜ M์˜ ๋ถ„ํ•ดํ•ฉ์ด N์ธ ๊ฒฝ์šฐ, M์„ N์˜ ์ƒ์„ฑ์ž๋ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 245์˜ ๋ถ„ํ•ดํ•ฉ์€ 256(=245+2+4+5)์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ 245๋Š” 256์˜ ์ƒ์„ฑ์ž๊ฐ€ ๋œ๋‹ค. ๋ฌผ๋ก , ์–ด๋–ค ์ž์—ฐ์ˆ˜์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ์ž์—ฐ์ˆ˜๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, N์˜ ๊ฐ€์žฅ ์ž‘์€ ์ƒ์„ฑ์ž๋ฅผ ๊ตฌํ•ด๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

# Input

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1 โ‰ค N โ‰ค 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค.

# Output

์ฒซ์งธ ์ค„์— ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ƒ์„ฑ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

# Example

# Input

216
1

# Output

198
1

# Solution

const input = require('fs').readFileSync('../input.txt').toString().trim();
const N = Number(input);

let result = 0;
for (let i = 0; i < N; i++) {
  let sum = 0;
  const str = i.toString();

  for (let j = 0; j < str.length; j++) {
    sum += Number(str[j]);
  }

  sum += i;
  if (sum == N) {
    result = i;
    break;
  }
}
console.log(result);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Last Updated: 2022. 6. 5. ์˜คํ›„ 3:42:39