문제풀이 2020 11 03
2020.11.03 연습
- 사용언어 : Javascript
- 테스트사이트 : Goorm
1 BinarySearch
/*
TEST DATA
5
1 5 7 9 10
9
5
1 5 7 9 10
8
*/
// Run by Node.js
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let countLine = 0;
let arrSize = 0;
let arr = [];
let num = 0;
function search() {
let low = 0;
let high = arrSize-1;
let mid = 0;
let check = 0;
while ( (answer = 'X') && low <= high ) {
mid = Math.floor((low + high)/2);
check = parseInt(arr[mid]);
if (check == num) {
return mid+1;
} else if (check > num ) {
high = mid-1;
} else {
low = mid+1;
}
}
return 'X';
}
rl.on("line", function(line) {
if (countLine == 0) {arrSize = line; countLine += 1; return;}
if (countLine == 1) {
arr = line.split(' ');
arr = arr.sort((a,b)=> {return a-b;});
countLine += 1;
return;
}
if (countLine == 2) {
num = parseInt(line);
console.log(search());
}
rl.close();
}).on("close", function() {
process.exit();
});
2 특정구간의 합
/*
TEST DATA
5
2 6 8 9 10
2 4
4
555 392 1823 234951
2 4
*/
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let countLine = 0;
let arr = [];
let start = 0;
let end = 0;
let answer = 0;
rl.on("line", function(line) {
if (countLine == 0) {
countLine += 1;
return;
}
if (countLine == 1) {
arr = line.split(' ');
countLine += 1;
return;
}
if (countLine == 2) {
start = parseInt(line.split(' ')[0]);
end = parseInt(line.split(' ')[1]);
while (start <= end) {
answer += parseInt(arr[start-1]);
start++;
}
}
console.log(answer);
rl.close();
}).on("close", function() {
process.exit();
});
3 배열 합치기
//TEST DATA로 했을땐 성공했는데 제출시 오답처리되었다..오류케이스 고민..ㅠ
/*
TEST DATA
5 4
2 3 6 7 8
1 8 9 11
5 4
1 2 3 4 5
6 7 8 9
*/
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let countLine = 0;
let arr1 = [];
let arr2 = [];
rl.on("line", function(line) {
if (countLine == 0) {
countLine += 1;
return;
}
if (countLine == 1) {
arr1 = line.split(' ');
countLine += 1;
return;
}
if (countLine == 2) {
arr2 = line.split(' ');
arr1 = arr1.concat(arr2).sort((a,b)=>{return a-b;});
console.log(arr1.toString().replace(/,/g,' '));
}
rl.close();
}).on("close", function() {
process.exit();
});