# [Algorithm/JS] ๋ฐฑ์ค€ 1065๋ฒˆ ํ•œ์ˆ˜

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

# Question

์–ด๋–ค ์–‘์˜ ์ •์ˆ˜ X์˜ ๊ฐ ์ž๋ฆฌ๊ฐ€ ๋“ฑ์ฐจ์ˆ˜์—ด์„ ์ด๋ฃฌ๋‹ค๋ฉด, ๊ทธ ์ˆ˜๋ฅผ ํ•œ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ๋“ฑ์ฐจ์ˆ˜์—ด์€ ์—ฐ์†๋œ ๋‘ ๊ฐœ์˜ ์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ์ผ์ •ํ•œ ์ˆ˜์—ด์„ ๋งํ•œ๋‹ค. N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ํ•œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

# Input

์ฒซ์งธ ์ค„์— 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค.

# Output

์ฒซ์งธ ์ค„์— 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ํ•œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

# Example

# Input 1

110
1

# Output 1

99
1

# Input 2

1
1

# Output 2

1
1

์ด ์™ธ์˜ ์ž…์ถœ๋ ฅ์€ ์ƒ๋‹จ์˜ ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ ๋งํฌ์—์„œ ํ™•์ธ

# Solution

# ๋‚˜์˜ ํ’€์ด

const fs = require('fs');
const input = fs.readFileSync('dev/stdin').toString().trim();
function d(n) {
  let count = 0;
  for (let i = 1; i <= n; i++) {
    let result = true;
    let preNum = 0;
    let nextNum = 0;
    let distance = 0;
    let initDistance = 0;

    if (i < 100) {
      count++;
    } else if (i < 1000) {
      let number = i;
      for (let j = 0; j < n.toString().length; j++) {
        preNum = number % 10;
        number = Math.floor(number / 10);
        nextNum = number % 10;
        if (j == 0) initDistance = preNum - nextNum;
        if (j < n.toString().length - 1) {
          distance = preNum - nextNum;
          if (initDistance != distance) result = false;
        }
      }
      if (result) count++;
    }
  }
  console.log(count);
}

d(Number(input));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

๋‚˜์˜ ํ’€์ด๋Š” ์ด์™€ ๊ฐ™๋‹ค. ๊ฒฐ๊ณผ๋Š”

ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ ์‹คํ–‰๊ฒฐ๊ณผ๋Š” ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ Output ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅํ•œ๋‹ค. ๋‚˜๋Š” ์œ„ ์ฝ”๋“œ๊ฐ€ ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋Š” ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ณ  ๊น”๋”ํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—..

# ์ˆ˜์ •ํ•œ ์ฝ”๋“œ









ย 
ย 
ย 









const input = require('fs').readFileSync('dev/stdin').toString().trim();

function solution(n) {
  let count = 0;
  for (let i = 1; i <= n; ++i) {
    if (i < 100) {
      count++;
    } else if (i < 1000) {
      const strNum = String(i);
      if (strNum[0] - strNum[1] === strNum[1] - strNum[2]) {
        count++;
      }
    }
  }
  console.log(count);
}

solution(Number(input));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์กŒ๋‹ค. 1๋ถ€ํ„ฐ 99๊นŒ์ง€๋Š” ๋ชจ๋‘ ํ•œ์ˆ˜์— ํ•ด๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์žกํ•œ ๋น„๊ต๋ฅผ ํ•  ํ•„์š” ์—†์ด count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

๊ฐ ์ƒํ™ฉ๋งˆ๋‹ค์˜ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”์—†์ด ํ•˜์ด๋ผ์ดํŠธ ๋œ ๋ผ์ธ์ฒ˜๋Ÿผ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ์ฐจ๋ฅผ ๋น„๊ตํ•ด ๊ฐ™๋‹ค๋ฉด count ๋ฅผ ์ฆ๊ฐ€ ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

์ฒ˜์Œ ์ œ์ถœํ•œ ์ฝ”๋“œ์™€ ๋น„๊ตํ•˜๋‹ˆ ์ด์ „ ์ฝ”๋“œ๊ฐ€ ํ˜•ํŽธ์—†๊ฒŒ ๋Š๊ปด์ง„๋‹ค.

Last Updated: 2022. 6. 5. ์˜คํ›„ 3:42:39