Development/Program Solving

[백준 단계별로 풀어보기] 5. 1차원 배열 (feat.NodeJS)

이쥬딩 2020. 12. 25. 00:23
728x90

10818번: 최소, 최대

 

10818번: 최소, 최대

첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.

www.acmicpc.net

CASE 1

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

const array = [];
const onInput = (input) => array.push(input);
const onClose = () => {
  const numbers = array[1].split(" ");
  let max = numbers[0];
  let min = numbers[0];
  for (let number of numbers) {
    number = Number(number);
    if(max < number){
      max = number;
    } else if(min > number){
      min = number;
    }
  }
  console.log(min, max);
  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

CASE 2

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

const array = [];
const onInput = (input) => array.push(input);
const onClose = () => {
  const numbers = array[1].split(" ");

  const max = Math.max(...numbers);
  const min = Math.min(...numbers);
  
  console.log(min, max);
  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

2562번: 최댓값

 

2562번: 최댓값

9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오. 예를 들어, 서로 다른 9개의 자연수 3, 29, 38, 12, 57, 74, 40, 85, 61 이 주어

www.acmicpc.net

CASE 1

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

const array = [];
const onInput = (input) => array.push(Number(input));

const onClose = () => {
  let max = array[0];
  
  for (let number of array) {
    if(max < number){
      max = number;
    }
  }

  console.log(`${max}\n${array.indexOf(max)+1}`);
  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

CASE 2

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

const array = [];
const onInput = (input) => array.push(Number(input));

const onClose = () => {
  const max = Math.max(...array);

  console.log(`${max}\n${array.indexOf(max)+1}`);
  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

2577번: 숫자의 개수

CASE 1

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

const array = [];
const onInput = (input) => array.push(Number(input));

const onClose = () => {
  const multiplication = array.reduce((pre, cur) => pre * cur);
  const numbers = String(multiplication).split("");
  for (let index = 0; index < 10; index++) {  
    const count = numbers.filter((number) => number == index);
    console.log(count.length);
  }
  
  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

CASE 2

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

const array = [];
const onInput = (input) => array.push(Number(input));

const onClose = () => {
  const counts = Array(10).fill(0);
  const multiNum = (array[0] * array[1] * array[2]).toString();

  for (const number in multiNum) {
    counts[Number(multiNum[number])]++;
  }
  
  counts.map((count) => console.log(count));

  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

CASE 3

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

const array = [];
const onInput = (input) => array.push(Number(input));

const onClose = () => {
  const counts = Array(10).fill(0);
  const multiNum = (array[0] * array[1] * array[2]).toString();

  for (const i in multiNum) {
    counts[Number(multiNum[i])]++;
  }

  for (let i = 0; i < 10; i++) {
    console.log(counts[i])
  }

  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

3개의 케이스에 따른 메모리와 시간은 아래와 같다.

거꾸로 보면 된다. CASE 1이 제일 아래!

시간복잡도는 다 같은데 왜 저런 결과가 나오는지 모르겠담...😭

복잡도 계산을 잘못하는건지 뭔지.....

아시는 분 계시면 제발 댓글로 알려주시면 감사하겠습니당 🙏

+ 같은 소스로 실행했을 때 다른 시간이 나오는거 보면 서버 상황마다 다르게 측정되나보담..

시간 의미가 없군....

그냥 시간 복잡도 계산해서 같으면 더 가독성 좋은 코드로 작성하는게 답인듯!

3052번: 나머지

 

3052번: 나머지

각 수를 42로 나눈 나머지는 39, 40, 41, 0, 1, 2, 40, 41, 0, 1이다. 서로 다른 값은 6개가 있다.

www.acmicpc.net

CASE 1

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

const array = [];
const onInput = (input) => array.push(Number(input));

const onClose = () => {
  const division = array.map((number) => number % 42);
  const deduplication = division.filter((number, index) => division.indexOf(number) === index);
  console.log(deduplication.length);

  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

1546번: 평균

 

1546번: 평균

첫째 줄에 시험 본 과목의 개수 N이 주어진다. 이 값은 1000보다 작거나 같다. 둘째 줄에 세준이의 현재 성적이 주어진다. 이 값은 100보다 작거나 같은 음이 아닌 정수이고, 적어도 하나의 값은 0보

www.acmicpc.net

CASE 1

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

const array = [];
const onInput = (input) => array.push(input);

const onClose = () => {
  const numbers = array[1].split(" ");
  const max = Math.max(...numbers);

  const calculation = numbers.reduce((pre, cur) =>  {
    return pre + (Number(cur) / max * 100);
  }, 0);

  console.log(calculation / numbers.length);
  process.exit();
}

rl.on('line', onInput)
  .on('close', onClose);

8958번: OX퀴즈

 

8958번: OX퀴즈

"OOXXOXXOOO"와 같은 OX퀴즈의 결과가 있다. O는 문제를 맞은 것이고, X는 문제를 틀린 것이다. 문제를 맞은 경우 그 문제의 점수는 그 문제까지 연속된 O의 개수가 된다. 예를 들어, 10번 문제의 점수

www.acmicpc.net

CASE 1

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

const array = [];
const onInput = (input) => array.push(input);

const onClose = () => {
  let count = 0;
  for (let i = 1; i <= array[0]; i++) {
    count = 0;
    const number = array[i].split("").map(value => {   
      if("O" === value) {
        count++;
      } else if ("X" === value) {
        count = 0;
      }
      return count; 
    });   
    console.log(number.reduce((pre, cur) => pre + cur));
  };
  process.exit();
};

rl.on('line', onInput)
  .on('close', onClose);

4344번: 평균은 넘겠지

 

4344번: 평균은 넘겠지

대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.

www.acmicpc.net

CASE 1

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

const array = [];
const onInput = (input) => array.push(input);
const onClose = () => {
  for(let i=1; i<=array[0]; i++) {
    const inputs = array[i].split(" ");
    const count = Number(inputs[0]);
    const numbers = inputs.slice(1);
    const average = numbers.reduce((pre, cur) => Number(pre) + Number(cur)) / count;
    const result = numbers.filter((value) => Number(value) > average);
    console.log(`${(result.length / count * 100).toFixed(3)}%`);
  };
  
  process.exit();
};

rl.on('line', onInput)
  .on('close', onClose);

 

더 나은 알고리즘이 있다면 댓글로 알려주세요! 🙏

728x90