728x90

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

 

😢 666, 1666, ..., 9666의 모습만 보면 안되고, 그 이상의 숫자를 예측해봐야 한다.

😊 다음과 같은 로직으로 문제를 해결했다.
1. 첫 번째 종말의 숫자는 666 이므로 665부터 숫자를 계속 증가시킨다.
2. 만약 증가시킨 숫자에 연속된 666이 포함된다? 그러면 input으로 받은 숫자를 1개만큼 줄인다.
3. input이 0이되면 증가를 중단하고 출력해준다.

 

728x90

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

Code

최종 제출 코드 1
최종 제출 코드 2

😢 조건문을 이용해 풀려다가 시간을 조금 보낸 문제

😊 일단, 다른 풀이를 참고하여 단순하게 풀어봤다.
맨 왼쪽 위칸이 'W'일 때 또는 'B'일 때, 8*8체스판을 미리 작성해두었다.
그리고 입력받은 체스판 크기만큼 8*8씩 잘라서 반복적으로 비교하여 모든 경우의 수를 저장했다.
최종적으로 Math.min()을 이용해 최솟값을 출력해주었다.

이런 저런 방법으로 다시 풀어봐야겠다.

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Re-painting the chess board
 
// 1st Solution
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
// const input = [
// '8 8',
// 'WBWBWBWB',
// 'BWBWBWBW',
// 'WBWBWBWB',
// 'BWBBBWBW',
// 'WBWBWBWB',
// 'BWBWBWBW',
// 'WBWBWBWB',
// 'BWBWBWBW'
// ];
 
const input = [
'10 13',
'BBBBBBBBWBWBW',
'BBBBBBBBBWBWB',
'BBBBBBBBWBWBW',
'BBBBBBBBBWBWB',
'BBBBBBBBWBWBW',
'BBBBBBBBBWBWB',
'BBBBBBBBWBWBW',
'BBBBBBBBBWBWB',
'WWWWWWWWWWBWB',
'WWWWWWWWWWBWB'
];
 
const NM = input
.shift()
.split(' ')
.map(num => parseInt(num));
const N = NM.shift();
const M = NM.shift();
const minArr = [];
 
const whiteFirst = [
'WBWBWBWB',
'BWBWBWBW',
'WBWBWBWB',
'BWBWBWBW',
'WBWBWBWB',
'BWBWBWBW',
'WBWBWBWB',
'BWBWBWBW'
];
 
const blackFirst = [
'BWBWBWBW',
'WBWBWBWB',
'BWBWBWBW',
'WBWBWBWB',
'BWBWBWBW',
'WBWBWBWB',
'BWBWBWBW',
'WBWBWBWB'
];
 
function whiteFirstPaint(y, x) {
let counter = 0;
 
for (let i = y; i < y + 8; i++)
for (let j = x; j < x + 8; j++)
if (input[i][j] !== whiteFirst[i - y][j - x]) counter++;
 
return counter;
}
 
function blackFirstPaint(y, x) {
let counter = 0;
 
for (let i = y; i < y + 8; i++)
for (let j = x; j < x + 8; j++)
if (input[i][j] !== blackFirst[i - y][j - x]) counter++;
 
return counter;
}
 
for (let i = 0; i + 7 < N; i++) {
for (let j = 0; j + 7 < M; j++) {
minArr.push(whiteFirstPaint(i, j));
minArr.push(blackFirstPaint(i, j));
}
}
 
console.log(Math.min.apply(null, minArr));
728x90

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

Code

최종 제출 코드

😢 '백준 4673번: 셀프넘버'를 풀었다면 쉽게 풀 수 있는 문제

😊 브루트 포스로 1부터 생성자가 있는 숫자들을 구해주고( d(n) ), N의 생성자가 여러 개라면 배열에 넣어준다.
최종적으로 생성자가 있다면 Math.min을 이용해 최솟값을 출력해주고, 없다면 0을 출력해주면 된다.
각 자릿수를 합하는 것은 위의 셀프넘버를 참고하면 된다.

참고로, 속도는 while로 자릿수를 나누는 게 더 빠르다(Full Code의 2nd Solution code 참고)

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Sum of break up(분해합)
 
// 1st Solution
 
// For submit
 
// const fs = require('fs');
// const input = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
 
// For local test
const input = 216;
const constructorArr = [];
 
function d(n) {
const N = n.toString().split('');
 
return n + N.reduce((acc, num) => (acc += parseInt(num)), 0);
}
 
for (let i = 1; i <= input; i++) {
if (d(i) === input) {
constructorArr.push(i);
}
}
 
if (constructorArr.length) {
console.log(Math.min.apply(null, constructorArr));
} else {
console.log(0);
}
 
// 2nd Solution
 
// For submit
 
// const fs = require('fs');
// const input = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
 
// For local test
// const input = 216;
// const constructorArr = [];
 
// function d(n) {
// let temp = n;
// let sum = n;
 
// while (temp) {
// sum += temp % 10;
// temp = parseInt(temp / 10);
// }
 
// return sum;
// }
 
// for (let i = 1; i <= input; i++) {
// if (d(i) === input) {
// constructorArr.push(i);
// }
// }
 
// if (constructorArr.length) {
// console.log(Math.min.apply(null, constructorArr));
// } else {
// console.log(0);
// }
728x90

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

Code

최종 제출 코드

😢 말 그대로 브루트 포스

😊 어떠한 요행을 바라기보다는 그냥 브루프 포스다.
반복문 돌려서 모든 경우의 수를 다 계산한다.

카드 3개의 조합의 합을 구해야 하므로 3개의 중첩 for loop을 이용해 해결 가능하다.

5 6 7 8 9 카드들을 위의 중첩문으로 실행하면 다음과 같은 순서로 진행된다.

[0][1][2] 5 6 7 = 18
[0][1][3] 5 6 8 = 19
[0][1][4] 5 6 9 = 20
[0][2][3] 5 7 8 = 20
[0][2][4] 5 7 9 = 21
[0][3][4] 5 8 9 = 22
[1][2][3] 6 7 8 = 21
[1][2][4] 6 7 9 = 22
[1][3][4] 6 8 9 = 23
[2][3][4] 7 8 9 = 24

위의 합 중에 M보다 작거나 같은 것은 21

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Blackjack
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
const input = ['5 21', '5 6 7 8 9'];
const NM = input
.shift()
.split(' ')
.map(num => parseInt(num));
const N = NM.shift();
const M = NM.shift();
const cardArr = input
.shift()
.split(' ')
.map(num => parseInt(num));
let max = 0;
 
for (let i = 0; i < N - 2; i++) {
for (let j = i + 1; j < N - 1; j++) {
for (let k = j + 1; k < N; k++) {
let sum = cardArr[i] + cardArr[j] + cardArr[k];
if (sum > max && sum <= M) {
max = sum;
}
}
}
}
 
console.log(max);
728x90

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

Code

최종 제출 코드

😢 기본 문제

😊 JavaScript built-in method인 sort()를 사용해주면 된다.
문자를 정렬할 때는 sort()를 그냥 써줘도 되지만, 숫자를 정렬할 때는 sort((a, b) => a-b); 로 써줘야 한다.
난 for ... of를 사용하고 좋아하지만, 속도(?)와 참고하는 사람들 때문에 정석 버전으로 캡쳐했다.

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Sort numbers(Ascending order)
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').map(num => parseInt(num));
 
// For local test
const input = [5, 5, 2, 3, 4, 1];
const N = input.shift();
const sorted = input.sort((a, b) => a - b);
 
for (let i = 0; i < N; i++) {
console.log(sorted[i]);
}
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').map(num => parseInt(num));
 
// For local test
// const input = [5, 5, 2, 3, 4, 1];
// input.shift();
// const sorted = input.sort((a, b) => a - b);
 
// for (let num of sorted) {
// console.log(num);
// }
728x90

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

Code

최종 제출 코드

😢 브루트 포스 감을 익힌 문제. 간단하다.

😊 2차원 배열로 키와 몸무게를 정렬해주고 중첩 for loop을 이용하면 된다.
첫 번째 사람부터 마지막 사람까지 본인보다 큰 몸무게&&키가 있다면 counter++; 해주고,
마지막 rank 배열에 저장할 때 +1을 한 번 더 해주면 그게 본인 순위가 된다.

모든 경우의 수를 비교하는 '브루트 포스(Brute-force)'

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// A big person
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
const input = ['5', '55 185', '58 183', '88 186', '60 175', '46 155'];
const T = input.shift();
const kgCmTable = input.map(kgCm => kgCm.split(' ').map(num => parseInt(num)));
const rank = [];
 
for (let i = 0; i < T; i++) {
let counter = 0;
for (let j = 0; j < T; j++) {
if (i !== j) {
if (
kgCmTable[i][0] < kgCmTable[j][0] &&
kgCmTable[i][1] < kgCmTable[j][1]
) {
counter++;
}
}
}
rank.push(counter + 1);
}
 
console.log(rank.join(' '));
728x90

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

Code

최종 제출 코드

😢 여러가지 고려해야할 경우를 찾아내는 것이 힘들었다.

😊
1. 원이 두 점에서 만나는 경우 (2)( r2 - r1 < d < r1 + r2)
2. 두 원이 외접하는 경우 (1)( d === r1 + r2 )
3. 두 원이 내접하는 경우 (1)( d === r2 - r1 && d !== 0 )
4. 한 원이 다른 원을 포함하는 경우 (0)( d < r2 - r1 )
5. 두 원이 떨어져 만나지 않는 경우 (0)( d > r1 + r2 )
6. 두 원이 일치하는 경우 (-1)( d === 0, r1 === r2 )

중점 사이의 거리를 구하고, 위 6가지 경우만 조건문으로 분류해주면 된다.

참고 자료 https://mathbang.net/101

 

두 원의 위치관계, 내접, 외접

위치관계 또 나오네요. 이번에는 두 원의 위치관계에요. 위치관계 마지막이니까 정신 바짝 차리고 따라오세요. 원과 직선의 위치관계, 원의 할선과 접선, 접점에서 했던 것처럼 두 원이 어떤 관계가 있는지 그때..

mathbang.net

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Turret
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
const input = ['3', '0 0 13 40 0 37', '0 0 3 0 7 4', '1 1 1 1 1 5'];
const T = parseInt(input.shift());
 
for (let i = 0; i < T; i++) {
const xyrxyr = input[i].split(' ').map(num => parseInt(num));
const x1 = xyrxyr.shift();
const y1 = xyrxyr.shift();
let r1 = xyrxyr.shift();
const x2 = xyrxyr.shift();
const y2 = xyrxyr.shift();
let r2 = xyrxyr.shift();
 
const dx = x1 - x2;
const dy = y1 - y2;
if (r1 > r2) {
// r1 <= r2로 정의
const temp = r1;
r1 = r2;
r2 = temp;
}
const rSum = (r1 + r2) * (r1 + r2);
const rSub = (r2 - r1) * (r2 - r1);
const d = dx * dx + dy * dy; // 중점 사이의 거리
 
// 1. 원이 두 점에서 만나는 경우 (두 점)(r2 - r1 < d < r1 + r2)
if (d < rSum && d > rSub) {
console.log(2);
// 2. 두 원이 외접하는 경우 (한 점)( d = r1 + r2)
// 3. 두 원이 내접하는 경우 (한 점)( d = r2 - r1 && d != 0)
} else if (d === rSum || (d === rSub && d !== 0)) {
console.log(1);
// 4. 하나의 원이 다른 원을 포함하는 경우 (못 만남)( d < r2 - r1 )
// 5. 두 원이 멀리 떨어져 만나지 않는 경우 (못 만남)( d > r1 + r2 )
} else if (d < rSub || d > rSum) {
console.log(0);
// 6. 두 원이 일치하는 경우 (무수히)( d = 0, r1 = r2 )
} else if (d === 0) {
if (r1 === r2) {
console.log(-1);
} else {
console.log(0);
}
}
}
728x90

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

Code

최종 제출 코드

😢 택시 기하학과 유클리드 기하학을 검색한 후, 이해하고 풀면 되는 문제

😊 유클리드 기하학에서 반지름이 R인 원이 넓이'반지름*반지름*PI'이므로 Math.powMath.PI를 이용하여 풀었고,
택시 기하학에서 반지름이 R인 원의 넓이마름모와 같으므로, '밑변*높이*2'(직사각형 넓이*2라고 생각)로 풀어주면 된다.

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Taxi geometry(Euclid geometry)
 
// For submit
 
// const fs = require('fs');
// const input = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
 
// For local test
const input = 1;
 
function Euclid(radius) {
return Math.pow(radius, 2) * Math.PI;
}
 
function taxi(radius) {
return Math.pow(radius, 2) * 2;
}
 
console.log(Euclid(input).toFixed(6));
console.log(taxi(input).toFixed(6));
728x90

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

Code

최종 제출 코드

😢 이쁘게 풀어주려다가 그냥 조건문으로 끝낸 문제

😊 가끔은 단순한게 좋은 방법인듯 하다.

일단 x좌표가 같은 것이 있다면, 이미 평행한 좌표가 있는 것이고
y좌표가 같은 것이 있다면, 역시 평행한 좌표가 있는 것이다.
따라서, x 또는 y가 한 번만 존재하는 좌표를 찾아내고 그 값을 넣어주면 된다.

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Fourth coordinate(in a rectangle)
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
const input = ['30 20', '10 10', '10 20'];
const strToInt = input.map(coords =>
coords.split(' ').map(num => parseInt(num))
);
 
let x = 0;
let y = 0;
if (strToInt[0][0] !== strToInt[1][0]) {
if (strToInt[0][0] !== strToInt[2][0]) {
x = strToInt[0][0];
} else {
x = strToInt[1][0];
}
} else {
x = strToInt[2][0];
}
 
if (strToInt[0][1] !== strToInt[1][1]) {
if (strToInt[0][1] !== strToInt[2][1]) {
y = strToInt[0][1];
} else {
y = strToInt[1][1];
}
} else {
y = strToInt[2][1];
}
 
console.log(`${x} ${y}`);
728x90

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

Code

최종 제출 코드

😢 변의 길이 순서가 랜덤으로 들어오는 듯 하다.

😊 ||(or)으로 모든 경우를 넣어주든지
sort순서를 처리해주면 된다.

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// A right-angled triangle(Pythagorean theorem)
 
// 2nd Soludion(sort)
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
const input = ['6 8 10', '25 52 60', '5 12 13', '5 4 3', '0 0 0'];
 
for (let sides of input) {
const strToInt = sides
.split(' ')
.map(num => Math.pow(parseInt(num), 2))
.sort((a, b) => a - b);
const firstSidePow = strToInt.shift();
const secondSidePow = strToInt.shift();
const thirdSidePow = strToInt.shift();
if (firstSidePow === 0 && secondSidePow === 0 && thirdSidePow === 0) {
break;
}
 
if (firstSidePow + secondSidePow === thirdSidePow) {
console.log('right');
} else {
console.log('wrong');
}
}
 
// 1st Solution(or)
 
// For submit
 
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
 
// For local test
// const input = ['6 8 10', '25 52 60', '5 12 13', '0 0 0'];
 
// for (let sides of input) {
// const strToInt = sides.split(' ').map(num => Math.pow(parseInt(num), 2));
// const firstSidePow = strToInt.shift();
// const secondSidePow = strToInt.shift();
// const thirdSidePow = strToInt.shift();
// if (firstSidePow === 0 && secondSidePow === 0 && thirdSidePow === 0) {
// break;
// }
 
// if (
// firstSidePow + secondSidePow === thirdSidePow ||
// firstSidePow + thirdSidePow === secondSidePow ||
// secondSidePow + thirdSidePow === firstSidePow
// ) {
// console.log('right');
// } else {
// console.log('wrong');
// }
// }

+ Recent posts