관리 메뉴

Jerry

백준 - 단계별 문제 - 기본 수학1 1/2 - node.js / javascript 본문

Problem Solving/Algorithm 문제 풀기

백준 - 단계별 문제 - 기본 수학1 1/2 - node.js / javascript

juicyjerry 2021. 7. 14. 01:13
반응형

 

 

 

손익 분기점

//! #1
/*
    A: 고정 비용, B: 가변 비용 C: 소비자 가격
    
    a + b <= c 지점
    1000 / 70 / 170
    9  : 1,000 / 630 / 1530
    10 : 1,000 / 700 / 1700
    11 : 1,000 / 770 / 1870
*/

let input = require("fs")
  .readFileSync("/dev/stdin")
  .toString()
  .split(" ")
  .map((el) => Number(el));
let [fixedPrice, variablePrice, customerPrice] = input;
let bep = 0;
let f = fixedPrice;
let v = 0;
let c = 0;

while (1) {
  console.log(f + v, c);
  if (f + v < c && bep !== 0) break;
  else if (f > 2100000000 || v > 2100000000 || c > 2100000000) {
    bep = -1;
    break;
  }

  //    f = fixedPrice * (bep + 1);
  v = variablePrice * (bep + 1);
  c = customerPrice * (bep + 1);

  bep++;
}

console.log(bep);

// #2
let input = require("fs")
  .readFileSync("/dev/stdin")
  .toString()
  .split(" ")
  .map((el) => Number(el));
let [f, v, c] = input;
let netProfit = c - v;

if (c <= v) {
  console.log(-1);
} else {
  console.log(Math.floor(f / netProfit) + 1);
}

 

 

 

 

 

 

 

분수 찾기

//! #1
let input = Number(require("fs").readFileSync("/dev/stdin").toString());
let cnt = 1;
let sum = 0;
let prevSum = 0;
/*
    1 : 1 (1/1) 1
    2 : 3 (1/2, 2/2, 2/1) 4
    3 : 5 (3/1, 3/2, 3/3, 2/3, 1/3) 9
    4 : 7 (1/4, 2/4, 3/4, 4/4, 4/3, 4/3, 4/2, 4/1) 16
    ...
*/

while (1) {
  sum += 2 * cnt++ - 1;
  if (sum > input) {
    prevSum -= 2 * (cnt - 1) - 1;
    cnt--;
    break;
  }
}

let left, right;
if (cnt % 2 === 0) {
  left = 1;
  right = cnt;

  while (prevSum < input) {
    if (left === cnt) {
      prevSum++;
      right--;
    } else {
      prevSum++;
      left++;
    }
  }
  console.log(Number(left) + "/" + Number(right));
} else {
  left = cnt;
  right = 1;

  while (sum < input) {
    sum++;
    left++;
  }
  console.log(Number(left) + "/" + Number(right));
}

//! #2
let input = Number(require("fs").readFileSync("/dev/stdin").toString());
let cnt = 1;
let sum = 0;
let prevSum = 0;
/*
    1 : 1 (1/1) 1
    2 : 2 (1/2, 2/1) 3
    3 : 3 (3/1, 2/2, 1/3) 6
    4 : 4 (1/4, 2/3, 3/2, 4/1) 10
    5 : 5 (5/1, 4/2, 3/3, 2/4, 1/5) 15
    ...
*/

while (1) {
  sum += cnt;
  if (sum >= input) {
    prevSum = sum - cnt;
    prevSum++;
    break;
  }
  cnt++;
}

let left, right;
if (cnt % 2 === 0) {
  left = 1;
  right = cnt;

  while (prevSum < input) {
    left++;
    right--;
    prevSum++;
  }

  console.log(Number(left) + "/" + Number(right));
} else {
  left = cnt;
  right = 1;

  while (prevSum < input) {
    left--;
    right++;
    prevSum++;
  }
  console.log(Number(left) + "/" + Number(right));
}

 

 

 

 

 

 

달팽이는 올라가고 싶다.

//! #1 (시간 초과)
/*
    up, down, height
    day++;
    
    if : up + down >= heigth => return day;
    else up + down & day++
    
*/

let input = require("fs")
  .readFileSync("/dev/stdin")
  .toString()
  .split(" ")
  .map((el) => Number(el));
let [up, down, height] = input;
let total = 0;
let day = 1;

while (total < height) {
  total += up - down;
  day++;
}

console.log(day);

//! #2
/*
    up, down, height
    day++;
    
    if : up + down >= heigth => return day;
    else up + down & day++
    
*/

let input = require("fs")
  .readFileSync("/dev/stdin")
  .toString()
  .split(" ")
  .map((el) => Number(el));
let [up, down, height] = input;

console.log(Math.ceil((height - down) / (up - down)));

 

 

 

 

 

 

벌집

/*
    1 : 1
    2-7 : 2
    8-19: : 3
    20-37 : 4
    38-61: 5
*/

let input = Number(require("fs").readFileSync("/dev/stdin").toString());
let n = 1;
let cnt = 1;

while (n < input) {
  n += cnt++ * 6;
}

console.log(cnt);

 

 

 

ACM 호텔

 

//! #1

/*
    방 번호: YXX / YYXX (Y || YY: 층 수, XX: 엘베부터 카운팅)
    걷는 거리가 같을 때, 아래층 방 선호
    모든 방이 비어있다고 가정
    
    N번째 도착 손님에게 배정될 방 번호 계산
    
    H: 6, W: 12
*/

let input = require("fs").readFileSync("/dev/stdin").toString().split("\n");
let [len, guest1Info, guest2Info] = input;
let [h1, w1, n1] = guest1Info.split(" ").map((el) => Number(el));
let [h2, w2, n2] = guest2Info.split(" ").map((el) => Number(el));
let room1, room2;
let cnt = 0;

for (let x = 1; x <= w1; x++) {
  for (let y = 1; y <= h1; y++) {
    cnt++;

    if (cnt === n1) {
      if (x < 2) {
        y = `0${y}`;
      } else if (x < 10) {
        x = `0${x}`;
      }
      room1 = `${y}${x}`;
    }
  }
}

cnt = 0;
for (let x = 1; x <= w1; x++) {
  for (let y = 1; y <= h1; y++) {
    cnt++;
    if (cnt === n2) {
      if (x < 2) {
        y = `0${y}`;
      } else if (x < 10) {
        x = `0${x}`;
      }
      room2 = `${y}${x}`;
    }
  }
}

console.log(room1);
console.log(room2);

//! #2
/*
    방 번호: YXX / YYXX (Y || YY: 층 수, XX: 엘베부터 카운팅)
    걷는 거리가 같을 때, 아래층 방 선호
    모든 방이 비어있다고 가정
    
    N번째 도착 손님에게 배정될 방 번호 계산
    
    H: 6, W: 12
*/

let input = require("fs").readFileSync("/dev/stdin").toString().split("\n");
let [len, guest1Info, guest2Info] = input;

for (let t = 0; t < len; t++) {
  let [h1, w1, n1] = guest1Info.split(" ").map((el) => Number(el));
  let [h2, w2, n2] = guest2Info.split(" ").map((el) => Number(el));
  let room1, room2;
  let cnt = 0;

  for (let x = 1; x <= w1; x++) {
    for (let y = 1; y <= h1; y++) {
      cnt++;

      if (cnt === n1) {
        if (x < 2) {
          y = `0${y}`;
        } else if (x < 10) {
          x = `0${x}`;
        }
        room1 = `${y}${x}`;
      }
    }
  }

  cnt = 0;
  for (let x = 1; x <= w1; x++) {
    for (let y = 1; y <= h1; y++) {
      cnt++;
      if (cnt === n2) {
        if (x < 2) {
          y = `0${y}`;
        } else if (x < 10) {
          x = `0${x}`;
        }
        room2 = `${y}${x}`;
      }
    }
  }

  console.log(room1);
  console.log(room2);
}

 

반응형