백준 단계별 - 1차원 배열

박상은·2021년 11월 2일
0

🤔 알고리즘 🤔

목록 보기
6/19

1. 10818번 - 최소, 최대

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];
let limit = 999;

rl.on("line", line => {
  input.push(line);
  limit = +input[0];
  if (input.length >= 2 && limit === input[1].split(" ").length) rl.close();
}).on("close", () => {
  let temp = input[1].split(" ").map(v => +v);
  let min = temp.reduce((prev, curr) => (prev < curr ? prev : curr));
  let max = temp.reduce((prev, curr) => (prev > curr ? prev : curr));

  console.log(min, max);

  process.exit();
});

2. 2562번 - 최대값

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];

rl.on("line", line => {
  input.push(+line);

  if (input.length === 9) rl.close();
}).on("close", () => {
  let answer = "";
  let maxNumber = input.reduce((prev, curr) => (prev < curr ? curr : prev));
  let maxIndex = input.indexOf(maxNumber) + 1;

  answer = `${maxNumber}\n${maxIndex}`;

  console.log(answer);

  process.exit();
});

3. 2577번 - 숫자의 개수

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];

rl.on("line", line => {
  input.push(+line);

  if (input.length === 3) rl.close();
}).on("close", () => {
  let answer = "";
  const array = Array(10)
    .fill()
    .map(() => 0);

  // 곱 -> 문자열변환 -> 배열로 분리 -> 해당 자릿수 개수 + 1
  String(input.reduce((prev, curr) => prev * curr, 1))
    .split("")
    .forEach(v => array[v]++);

  array.forEach(v => (answer += `${v}\n`));

  console.log(answer);

  process.exit();
});

4. 3052번 - 나머지

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];

rl.on("line", line => {
  input.push(+line);

  if (input.length === 10) rl.close();
}).on("close", () => {
  // Set으로 중복제거
  const result = new Set(input.map(v => v % 42)).size;

  console.log(result);

  process.exit();
});

5. 1546번 - 평균

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];

rl.on("line", line => {
  input.push(line);

  if (input.length === 2 && input[1].split(" ").length === +input[0]) rl.close();
}).on("close", () => {
  const inputArray = input[1].split(" ").map(v => +v);
  const maxScore = inputArray.reduce((prev, curr) => (prev < +curr ? +curr : prev), 0);
  const avg = inputArray.reduce((prev, curr) => (curr / maxScore) * 100 + prev, 0) / inputArray.length;

  console.log(avg);

  process.exit();
});

6. 8958번 - OX퀴즈

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];

rl.on("line", line => {
  input.push(line);

  if (input.length >= 2 && input.length - 1 === +input[0]) rl.close();
}).on("close", () => {
  input.shift();
  let answer = "";
  let currentScore = 1;

  let result = input.map(value => {
    currentScore = 1;
    return value.split("").reduce((prev, curr) => {
      switch (curr) {
        case "O":
          return prev + currentScore++;
        case "X":
          currentScore = 1;
          return prev;
      }
    }, 0);
  });

  result.forEach(v => (answer += `${v}\n`));

  console.log(answer);

  process.exit();
});

7. 4344번 - 평균은 넘겠지

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];

rl.on("line", line => {
  input.push(line);

  if (input.length >= 2 && input.length - 1 === +input[0]) rl.close();
}).on("close", () => {
  input.shift();
  let answer = "";

  // 맨 앞에 N제거
  const inputArray = input.map(v => v.split(" ").slice(1));

  // 각각의 평균
  const avgArray = inputArray.map(v => v.reduce((prev, curr) => prev + +curr, 0) / v.length);

  // 평균 넘는 학생들의 비율
  const studentList = inputArray.map((value, i) => value.reduce((prev, curr) => (+curr > avgArray[i] ? prev + 1 : prev), 0) / value.length);

  studentList.forEach(v => (answer += `${(v * 100).toFixed(3)}%\n`));

  console.log(answer);

  process.exit();
});

0개의 댓글