[백준10093_자바스크립트(javascript)] - 숫자

경이·2024년 1월 9일

𝑩𝑶𝑱 (𝒋𝒔)

목록 보기
18/325

🔴 문제

숫자


🟡 Sol

const fs = require('fs');
const input = fs
  .readFileSync('/dev/stdin')
  .toString()
  .trim()
  .split(' ')
  .map((it) => BigInt(it));

let a, b;

if (input[0] < input[1]) {
  a = input[0];
  b = input[1];
} else {
  a = input[1];
  b = input[0];
}

const answer = [];
for (let i = a + 1n; i < b; i++) answer.push(i.toString());

if (a !== b) {
  console.log((b - a - 1n).toString());
  console.log(...answer);
} else {
  console.log(0);
}

🟢 풀이

나는 bigint썼는데 다른 스터디원 말을 들어보니 안써도 된다고 하신다.
어찌되었던, 숫자 뒤에 n을 붙여주면 BigInt를 다룰수 있다.

아래는 같이 알고리즘 문제풀이를 진행하는 @정욱님 의 소스코드인데 bigint를 사용하지 않은 모습!

const fs = require('fs');

const input = fs.readFileSync('/dev/stdin').toString();
const numbers = input.split(' ').map(str => Number.parseInt(str, 10));
const sorted = [...numbers];
sorted.sort((a, b) => a - b);

const [first, second] = sorted;

if (first === second) {
  console.log(0);
} else {
  console.log(second - first - 1); 
}

for (let i = first + 1; i < second; i++) {
  process.stdout.write(`${i} `);
}

🔵 Ref

https://github.com/FEDeepDive/CodingTest/blob/hatchling13/01.%20%EA%B8%B0%EC%B4%88%EC%BD%94%EB%93%9C%20%EC%9E%91%EC%84%B1%20%EC%9A%94%EB%A0%B9/%EC%88%AB%EC%9E%90.js

profile
록타르오가르

0개의 댓글