Skip to content

Commit

Permalink
✨ 202106134- 백준 알고리즘(12문제)
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyunan-dev committed Jun 14, 2021
1 parent 285d633 commit 2029ca1
Show file tree
Hide file tree
Showing 13 changed files with 590 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 210614/1011_Alpha_Centauri.js
@@ -0,0 +1,44 @@
// No.1011 Fly me to the Alpha Centauri
// https://www.acmicpc.net/problem/1011 문제 링크 (문제가 너무 길다... 이미지도 있음...)

// 풀이 과정
// 주어진 x, y 사이의 거리를 N이라고 하면,
// √N 보다 작은 정수 중 가장 가까운 수 n을 가지고 (n의 제곱 + (n+1)의 제곱 ) / 2를 해서 그 값을 average라고 한다.
// average보다 N이 작으면, 2n만큼이 최솟값이 되고
// average보다 N이 크면, 2n+1이 최솟값이 된다. (이 때 average는 절대 정수가 나올 수 있으므로 같을 때는 고려할 필요가 없다.)
// 예외로 n의 제곱수가 N인 경우에는 최솟값이 2n - 1이 된다.

// let fs = require("fs");
// let input = fs
// .readFileSync("/dev/stdin")
// .toString()
// .split("\n");

let input = ["3", "0 3", "1 5", "45 50"];

let caseNum = Number(input[0]);

for (let i = 1; i <= caseNum; i++) {
let [x, y] = input[i].split(" ").map((num) => Number(num));
let distance = y - x;
console.log(getMinWork(distance));
}

function getMinWork(distance) {
let minWorkNum;
let squareRoot = Math.sqrt(distance);
let intSquareRoot = parseInt(squareRoot);
if (squareRoot === intSquareRoot) {
minWorkNum = intSquareRoot * 2 - 1;
return minWorkNum;
}

let average = (intSquareRoot ** 2 + (intSquareRoot + 1) ** 2) / 2; // 절대 정수로 안나옴:)
if (distance < average) {
minWorkNum = 2 * intSquareRoot;
} else {
minWorkNum = 2 * intSquareRoot + 1;
}

return minWorkNum;
}
29 changes: 29 additions & 0 deletions 210614/10869_사칙연산.js
@@ -0,0 +1,29 @@
// 사칙연산
// 두 자연수 A와 B가 주어진다. 이때, A+B, A-B, A*B, A/B(몫), A%B(나머지)를 출력하는 프로그램을 작성하시오.

// 입력 : 두 자연수 A와 B가 주어진다. (1 ≤ A, B ≤ 10,000)
// 출력 : 첫째 줄에 A+B, 둘째 줄에 A-B, 셋째 줄에 A*B, 넷째 줄에 A/B, 다섯째 줄에 A%B를 출력한다.

let fs = require("fs");
const input = fs
.readFileSync("/dev/stdin")
.toString()
.split(" ")
.map(function (a) {
return +a;
});

let x = input[0];
let y = input[1];

let sum = x + y;
let minus = x - y;
let multiple = x * y;
let share = Math.floor(x / y);
let remainder = x % y;

console.log(sum);
console.log(minus);
console.log(multiple);
console.log(share);
console.log(remainder);
36 changes: 36 additions & 0 deletions 210614/1110_더하기사이클.js
@@ -0,0 +1,36 @@
// No.1110 더하기사이클
// 0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다.
// 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음, 주어진 수의 가장 오른쪽 자리 수와 앞에서 구한 합의 가장 오른쪽 자리 수를 이어 붙이면 새로운 수를 만들 수 있다.
// 다음 예를 보자.
// 26부터 시작한다.
// 2+6 = 8이다. 새로운 수는 68이다.
// 6+8 = 14이다. 새로운 수는 84이다.
// 8+4 = 12이다. 새로운 수는 42이다.
// 4+2 = 6이다. 새로운 수는 26이다.
// 위의 예는 4번만에 원래 수로 돌아올 수 있다.
// 따라서 26의 사이클의 길이는 4이다.
// N이 주어졌을 때, N의 사이클의 길이를 구하는 프로그램을 작성하시오.

let N = 55;

function plusCycle(N) {
let num = String(N);
let count = 0;

do {
let numAry = num.split("").map((num) => Number(num));
let target1 = numAry[numAry.length - 1];
let sum = 0;
numAry.forEach((n) => (sum += n));

let resultAry = String(sum).split("");
let target2 = resultAry[resultAry.length - 1];

num = String(target1 * 10 + Number(target2));
count++;
} while (N !== Number(num));

return count;
}

console.log(plusCycle(N));
46 changes: 46 additions & 0 deletions 210614/1157_단어공부.js
@@ -0,0 +1,46 @@
"use strict";
// No.1157 단어공부
// 알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

// 입력 : 첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
// 출력 : 첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString();

function studyWord(word) {
const alphabet = new Array(26).fill(0); // 알파벳 공간 만들기

let charAry = word.split(""); // 하나씩 쪼갬
let asciiUpperA = "A".charCodeAt();
let asciiLowerA = "a".charCodeAt();

charAry.forEach((char) => {
let charAscii = char.charCodeAt();
let index = 0;
if (charAscii >= 65 && charAscii <= 90) {
index = charAscii - asciiUpperA;
} else {
index = charAscii - asciiLowerA;
}
alphabet[index] += 1;
});

let maxNum = 0;
let maxIndex = 0;
let double = false;

for (let i = 0; i < alphabet.length; i++) {
if (maxNum < alphabet[i]) {
maxNum = alphabet[i];
maxIndex = i;
double = false;
} else if (maxNum === alphabet[i]) {
double = true;
}
}

return double === true ? "?" : String.fromCharCode(maxIndex + asciiUpperA);
}

console.log(studyWord(input));
27 changes: 27 additions & 0 deletions 210614/1157_단어공부_refactoring.js
@@ -0,0 +1,27 @@
const input = "baaa";

function studyWord(word) {
const alphabet = new Array(26).fill(0);

let charAry = word.toLowerCase().split("");
let asciiA = "a".charCodeAt();

charAry.forEach((char) => {
let asciiChar = char.charCodeAt();
let index = asciiChar - asciiA;
alphabet[index] += 1;
});

let maxNum = Math.max(...alphabet);
let maxIndex = alphabet.indexOf(maxNum);
let isSame = false;

for (let i = 0; i < alphabet.length; i++) {
if (maxNum === alphabet[i] && i !== maxIndex) {
isSame = true;
}
}
return isSame ? "?" : String.fromCharCode(maxIndex + asciiA).toUpperCase();
}

console.log(studyWord(input));
36 changes: 36 additions & 0 deletions 210614/1316_그룹단어체크.js
@@ -0,0 +1,36 @@
// No.1316 그룹 단어 체크
// 그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때문에 그룹 단어이지만, aabbbccb는 b가 떨어져서 나타나기 때문에 그룹 단어가 아니다.

// 단어 N개를 입력으로 받아 그룹 단어의 개수를 출력하는 프로그램을 작성하시오.

// ⭐입력 : 첫째 줄에 단어의 개수 N이 들어온다. N은 100보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 단어가 들어온다. 단어는 알파벳 소문자로만 되어있고 중복되지 않으며, 길이는 최대 100이다.

// ⭐출력 : 첫째 줄에 그룹 단어의 개수를 출력한다.

const input = ["4", "aba", "abab", "abcabc", "a"]; // 1

// let fs = require("fs");
// let input = fs.readFileSync("/dev/stdin").toString().split("\n");

const lowerAsciiA = "a".charCodeAt();

let caseNum = Number(input[0]);
let count = 0;
for (let i = 1; i <= caseNum; i++) {
checkGroupWord(input[i]) === true ? (count += 1) : null;
}

function checkGroupWord(word) {
const alphabet = new Array(26).fill(false);
let currentChar = "";
let isGroupWord = true;
for (let i of word) {
let index = i.charCodeAt() - lowerAsciiA;
if (currentChar !== i && alphabet[index] === true) isGroupWord = false;
alphabet[index] = true;
currentChar = i;
}
return isGroupWord;
}

console.log(count);
32 changes: 32 additions & 0 deletions 210614/2588_곱셈.js
@@ -0,0 +1,32 @@
// No.2588 곱셈
// (세 자리 수) × (세 자리 수)는 다음과 같은 과정을 통하여 이루어진다.
// (1)과 (2)위치에 들어갈 세 자리 자연수가 주어질 때 (3), (4), (5), (6)위치에 들어갈 값을 구하는 프로그램을 작성하시오.

// 입력 : 첫째 줄에 (1)의 위치에 들어갈 세 자리 자연수가, 둘째 줄에 (2)의 위치에 들어갈 세자리 자연수가 주어진다.
// 출력 : 첫째 줄부터 넷째 줄까지 차례대로 (3), (4), (5), (6)에 들어갈 값을 출력한다.

/* 가짜 데이터 */
// let x = "472";
// let y = "385";

/* --------- */

// let fs = require("fs");
// let [x, y] = fs.readFileSync("/dev/stdin").toString().split("\n");

function multiple(x, y) {
let numX = Number(x);
let yAry = y.split("");

let third = numX * Number(yAry[2]);
let fourth = numX * Number(yAry[1]);
let fifth = numX * Number(yAry[0]);
let result = fifth * 100 + fourth * 10 + third;

console.log(third);
console.log(fourth);
console.log(fifth);
console.log(result);
}

multiple(x, y);
28 changes: 28 additions & 0 deletions 210614/2839_설탕배달.js
@@ -0,0 +1,28 @@
// No.2839 설탕배달
// 상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그램 봉지와 5킬로그램 봉지가 있다.

// 상근이는 귀찮기 때문에, 최대한 적은 봉지를 들고 가려고 한다. 예를 들어, 18킬로그램 설탕을 배달해야 할 때, 3킬로그램 봉지 6개를 가져가도 되지만, 5킬로그램 3개와 3킬로그램 1개를 배달하면, 더 적은 개수의 봉지를 배달할 수 있다.

// 상근이가 설탕을 정확하게 N킬로그램 배달해야 할 때, 봉지 몇 개를 가져가면 되는지 그 수를 구하는 프로그램을 작성하시오.

// 입력: 첫째 줄에 N이 주어진다. (3 ≤ N ≤ 5000)
// 출력: 상근이가 배달하는 봉지의 최소 개수를 출력한다. 만약, 정확하게 N킬로그램을 만들 수 없다면 -1을 출력한다.

const N = 18;

function getBagNum(weight) {
let fiveLimit = parseInt(weight / 5);
let count = -1;

for (let i = fiveLimit; i >= 0; i--) {
let currentWeight = weight;
currentWeight -= i * 5;
if (currentWeight % 3 === 0) {
count = i + currentWeight / 3;
break;
}
}
return count;
}

console.log(getBagNum(11));
38 changes: 38 additions & 0 deletions 210614/2884_알람시계.js
@@ -0,0 +1,38 @@
// // No.2884 알람시계
// 상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다.
// 상근이는 모든 방법을 동원해보았지만, 조금만 더 자려는 마음은 그 어떤 것도 없앨 수가 없었다.
// 이런 상근이를 불쌍하게 보던, 창영이는 자신이 사용하는 방법을 추천해 주었다.
// 바로 "45분 일찍 알람 설정하기"이다.
// 이 방법은 단순하다. 원래 설정되어 있는 알람을 45분 앞서는 시간으로 바꾸는 것이다. 어차피 알람 소리를 들으면, 알람을 끄고 조금 더 잘 것이기 때문이다. 이 방법을 사용하면, 매일 아침 더 잤다는 기분을 느낄 수 있고, 학교도 지각하지 않게 된다.
// 현재 상근이가 설정한 알람 시각이 주어졌을 때, 창영이의 방법을 사용한다면, 이를 언제로 고쳐야 하는지 구하는 프로그램을 작성하시오.

// ✨입력 :첫째 줄에 두 정수 H와 M이 주어진다. (0 ≤ H ≤ 23, 0 ≤ M ≤ 59) 그리고 이것은 현재 상근이가 설정한 놓은 알람 시간 H시 M분을 의미한다.
// 입력 시간은 24시간 표현을 사용한다. 24시간 표현에서 하루의 시작은 0:0(자정)이고, 끝은 23:59(다음날 자정 1분 전)이다. 시간을 나타낼 때, 불필요한 0은 사용하지 않는다.

// 출력 : 첫째 줄에 상근이가 창영이의 방법을 사용할 때, 설정해야 하는 알람 시간을 출력한다. (입력과 같은 형태로 출력하면 된다.)

// let fs = require("fs");
// let [hours, minutes] = fs
// .readFileSync("/dev/stdin")
// .toString()
// .split(" ")
// .map(function (a) {
// return +a;
// });

function getTime(hours, minutes) {
if (minutes >= 45) {
minutes -= 45;
} else {
if (hours === 0) {
hours = 23;
minutes += 15;
} else {
hours -= 1;
minutes += 15;
}
}
console.log(hours, minutes);
}

getTime(hours, minutes);

0 comments on commit 2029ca1

Please sign in to comment.