5
R R R U D D
const dx = [0, 0, -1, 1];
const dy = [-1, 1, 0, 0];
const moveTypes = ["L", "R", "U", "D"];
if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input.push(line);
}).on("close", function () {
solution();
process.exit();
});
const solution = () => {
const dx = [0, 0, -1, 1];
const dy = [-1, 1, 0, 0];
const moveTypes = ["L", "R", "U", "D"];
let n = parseInt(input[0]);
let arr = input[1].split(" ");
let x = (y = 1);
let nx = (ny = 0);
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < moveTypes.length; j++) {
if (arr[i] == moveTypes[j]) {
nx = x + dx[j];
ny = y + dy[j];
}
if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
[x, y] = [nx, ny];
}
}
console.log(x, y);
};
let nx;
nx + 3;
// nx == NaN
let nx = 0;
nx + 3;
// nx == 3;
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input.push(line);
}).on("close", function () {
solution();
process.exit();
});
const solution = () => {
let n = parseInt(input[0]);
let arr = input[1].split(" ");
let [x, y] = [1, 1];
for (let i = 0; i < arr.length; i++) {
if (arr[i] == "R") {
let nx = x + 1;
if (nx <= n) [x, y] = [nx, y];
} else if (arr[i] == "L") {
let nx = x - 1;
if (nx > 0) [x, y] = [nx, y];
} else if (arr[i] == "U") {
let ny = y - 1;
if (ny > 0) [x, y] = [x, ny];
} else if (arr[i] == "D") {
let ny = y + 1;
if (ny <= n) [x, y] = [x, ny];
}
}
console.log(x, y);
};
확인해야 할 전체 데이터의 개수가 100만 개 이하일 때 완전 탐색을 사용하면 적절하다.
1. 숫자가 아니라 문자열로 받기
2.
3.
4.
...
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = 0;
rl.on("line", function (line) {
input += line;
}).on("close", function () {
solution();
process.exit();
});
const solution = () => {
let count = 0;
for (let i = 0; i <= input; i++) {
for (let j = 0; j < 60; j++) {
for (let k = 0; k < 60; k++) {
//빈 문자열""에 더하지 않고 toString()을 사용한다.
//let time = "";
//time += i + j + k;
let time = i.toString() + j.toString() + k.toString();
if (time.includes("3")) count++;
}
}
}
console.log(count);
};
string형태로 수를 더하고 싶다면 toString()을 쓰자.
let time = i.toString() + j.toString() + k.toString();
""에 더한다면, 1, 2, 3을 다 합한 값인 6을 string형태로 바꿔준다.
let time = "";
time += 1 + 2 + 3;
// 6
// typeof time은 string
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = "";
rl.on("line", function (line) {
input += line;
}).on("close", function () {
solution();
process.exit();
});
const solution = () => {
const steps = [
[-2, -1],
[-1, -2],
[1, -2],
[2, -1],
[2, 1],
[1, 2],
[-1, 2],
[-2, 1],
];
const small = Array.from({ length: 8 }, (v, i) =>
String.fromCharCode(i + 97)
);
let answer = 0;
let pointArr = input.split("");
let [x, y] = [parseInt(pointArr[1]), small.indexOf(pointArr[0]) + 1];
for (let stepType of steps) {
let nx = x + stepType[0];
let ny = y + stepType[1];
if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
answer++;
}
}
console.log(answer);
};
4 4
1 1 0
1 1 1 1
1 0 0 1
1 1 0 1
1 1 1 1
// Run by Node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
input.push(line);
}).on("close", function () {
solution();
process.exit();
});
const solution = () => {
let arr = [];
console.log(input);
for (let x of input) {
arr.push(x.split(" "));
}
for (let x of arr) {
for (let i = 0; i < x.length; i++) {
x[i] = parseInt(x[i]);
}
}
const firstLine = arr.shift();
const secondLine = arr.shift();
let [n, m] = [firstLine[0], firstLine[1]];
let [x, y] = [secondLine[0], secondLine[1]];
let direction = secondLine[2];
let ch = Array.from(Array(n), () => Array(m).fill(0));
let dx = [-1, 0, 1, 0];
let dy = [0, 1, 0, -1];
const turnLeft = () => {
}
};
K1KA5CB7
AJKDLSI412K4JSJ9D
// Run by Node.js
const { count } = require("console");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let inputStr = "";
rl.on("line", function (line) {
inputStr += line;
}).on("close", function () {
solution();
process.exit();
});
const solution = () => {
let arr = inputStr.split("");
let num = 0;
let str = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].charCodeAt() >= 65) {
str.push(arr[i]);
} else num = num + parseInt(arr[i]);
}
str.sort();
let answer = str.join("") + num;
console.log(answer);
};
// Run by Node.js
const { count } = require("console");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let s = "";
rl.on("line", function (line) {
s += line;
}).on("close", function () {
solution();
process.exit();
});
function solution() {
let n = s.length;
let answer = s.length;
for (let step = 1; step <= parseInt(n / 2); step++) {
let compressed = "";
let prev = s.substr(0, step);
let cnt = 1;
for (let j = step; j < n; j += step) {
if (prev == s.substr(j, step)) cnt++;
else {
compressed += cnt >= 2 ? String(cnt) + prev : prev;
prev = s.substr(j, step);
cnt = 1;
}
}
compressed += cnt >= 2 ? String(cnt) + prev : prev;
answer = Math.min(answer, compressed.length);
}
console.log(answer);
//return answer;
}
substr(잘라낼startIndex, 잘라낼길이);
substring(잘라낼startIndex, 잘라낼lastIndex);
전체 코드