Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[๋ฐฑ์ค€ 10773๋ฒˆ] ์ œ๋กœ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 10773๋ฒˆ] ์ œ๋กœ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 1. 20. 16:44
728x90
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/10773

 

10773๋ฒˆ: ์ œ๋กœ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ •์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ 100,000) ์ดํ›„ K๊ฐœ์˜ ์ค„์— ์ •์ˆ˜๊ฐ€ 1๊ฐœ์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ •์ˆ˜๋Š” 0์—์„œ 1,000,000 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ์ •์ˆ˜๊ฐ€ "0" ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ์“ด ์ˆ˜๋ฅผ ์ง€์šฐ๊ณ , ์•„๋‹ ๊ฒฝ

www.acmicpc.net

 

const fs = require('fs');
let input = fs
  .readFileSync('dev/stdin')
  .toString()
  .trim()
  .split('\n')
  .map((v) => +v);
input.shift();

const answer = [];

input.filter((v) => {
  if (v === 0) {
    answer.pop();
  } else {
    answer.push(v);
  }
});

console.log(answer.reduce((prev, cur) => prev + cur, 0));

๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด๋ฉฐ, reduce๋ฅผ ์ด์šฉํ•ด ์ถœ๋ ฅ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•ด๋‹น ๋ฌธ์ œ์— ๋‚˜์˜จ ์ตœ๋Œ€ ์ถœ๋ ฅ๊ฐ’ 2^31 - 1์€ C๋˜๋Š” JAVA๋“ฑ ์–ธ์–ด์—์„œ int์˜ ์ตœ๋Œ€ ๋ฒ”์œ„์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. 

728x90
๋ฐ˜์‘ํ˜•
Comments