Js정리
정수의 길이
// Run by Node.js
const readline = require('readline');
(async () => {
let rl = readline.createInterface({ input: process.stdin });
for await (const line of rl) {
console.log(line.length);
rl.close();
}
process.exit();
})();
대소문자 바꾸기
// Run by Node.js
const readline = require('readline');
(async () => {
let rl = readline.createInterface({ input: process.stdin });
const lines = [];
for await (const line of rl) {
lines.push(line);
if (lines.length === 2) {
rl.close();
}
}
const length = parseInt(lines[0]);
const input = lines[1];
let output = '';
for (let i = 0; i < length; i++) {
if (input[i] === input[i].toUpperCase()) {
output += input[i].toLowerCase();
} else if (input[i] === input[i].toLowerCase()) {
output += input[i].toUpperCase();
}
}
console.log(output);
process.exit();
})();
A + B(2)
// Run by Node.js
const readline = require('readline');
(async () => {
let rl = readline.createInterface({ input: process.stdin });
for await (const line of rl) {
let input = line.split(' ');
let A = parseFloat(input[0]);
let B = parseFloat(input[1]);
// A와 B의 합을 소수점 6자리까지 출력
console.log((A + B).toFixed(6));
rl.close();
}
process.exit();
})();
A + B
// Run by Node.js
const readline = require('readline');
(async () => {
let rl = readline.createInterface({ input: process.stdin });
for await (const line of rl) {
let str = line.split(' ');
console.log(parseInt(str[0]) + parseInt(str[1]));
rl.close();
}
process.exit();
})();
큰 팩토리얼
// Node.js에서 실행
const readline = require('readline');
(async () => {
let rl = readline.createInterface({ input: process.stdin });
for await (const line of rl) {
const input = parseInt(line);
let output = 1n;
const MOD = 1000000007n;
for (let i = BigInt(input); i >= 1; i--) {
output = (output * i) % MOD;
}
console.log(output.toString()); // BigInt를 문자열로 변환하여 출력
rl.close();
}
process.exit();
})();
숫자 제거 배열
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', (line) => {
input.push(line);
});
rl.on('close', () => {
const [N, K] = input[0].split(' ');
const numbers = input[1].split(' ').map(Number);
let count = 0;
for (const num of numbers) {
if (!num.toString().includes(K.toString())) {
count++;
}
}
console.log(count);
});
거스름 돈
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('', (input) => {
input = parseInt(input);
let count = 0;
while (input !== 0) {
if (input >= 40) {
input -= 40;
count++;
} else if (input >= 20) {
input -= 20;
count++;
} else if (input >= 10) {
input -= 10;
count++;
} else if (input >= 5) {
input -= 5;
count++;
} else if (input >= 1) {
input -= 1;
count++;
}
}
console.log(count);
rl.close();
});
인공지능 청소기
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputLines = [];
let currentLine = 0;
rl.on('line', (line) => {
inputLines.push(line);
});
rl.on('close', () => {
main();
});
function readLine() {
return inputLines[currentLine++];
}
function main() {
const T = parseInt(readLine());
let result = "";
for (let i = 0; i < T; i++) {
const Line = readLine();
const arr = Line.split(" ");
const X = parseInt(arr[0]);
const Y = parseInt(arr[1]);
const N = parseInt(arr[2]);
const sum = Math.abs(X) + Math.abs(Y);
if (sum <= N && (sum - N) % 2 === 0) {
result = "YES";
console.log(result);
} else {
result = "NO";
console.log(result);
}
}
}
소금물의 농도 구하기
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('', (line) => {
const input = line.split(' ').map(Number);
const N = input[0];
const M = input[1];
const salt = (7 * N) / 100;
const nongdo = (salt / (N + M)) * 100;
// 소수점 두 자리까지 내림 처리
const flooredValue = Math.floor(nongdo * 100) / 100;
// 형식 지정
console.log(flooredValue.toFixed(2));
rl.close();
});
딱지놀이
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputLines = [];
let currentLine = 0;
rl.on('line', (line) => {
inputLines.push(line);
});
rl.on('close', () => {
main();
});
function main() {
const N = parseInt(inputLines[currentLine++]);
for (let i = 0; i < N; i++) {
let LineA = inputLines[currentLine++];
let arrA = new Array(5).fill(0); // Initialize array to hold counts of 0, 1, 2, 3, 4 (index 0 will remain unused)
let tokensA = LineA.split(" ");
let countA = parseInt(tokensA[0]);
for (let j = 1; j <= countA; j++) {
let num = parseInt(tokensA[j]);
arrA[num]++;
}
let LineB = inputLines[currentLine++];
let arrB = new Array(5).fill(0); // Initialize array to hold counts of 0, 1, 2, 3, 4 (index 0 will remain unused)
let tokensB = LineB.split(" ");
let countB = parseInt(tokensB[0]);
for (let j = 1; j <= countB; j++) {
let num = parseInt(tokensB[j]);
arrB[num]++;
}
let flag = false;
for (let j = 4; j > 0; j--) {
if (arrA[j] > arrB[j]) {
console.log("A");
break;
} else if (arrA[j] < arrB[j]) {
console.log("B");
break;
}
if (j === 1 && arrA[j] === arrB[j]) {
flag = true;
}
}
// If it's a draw
if (flag) {
console.log("D");
}
}
console.log();
}
계수기 만들기
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', (line) => {
input.push(line);
if (input.length === 4) {
rl.close();
}
});
rl.on('close', () => {
const N = parseInt(input[0]);
const ArrE = input[1].split(' ').map(Number);
const ArrS = input[2].split(' ').map(Number);
const K = parseInt(input[3]);
for (let i = 0; i < K; i++) {
ArrS[ArrS.length - 1]++;
for (let j = ArrS.length - 1; j >= 0; j--) {
if (ArrS[j] > ArrE[j]) {
ArrS[j] = 0;
if (j > 0) {
ArrS[j - 1]++;
}
}
}
}
console.log(ArrS.join(''));
});
어려운 문제
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('', (input) => {
const N = parseInt(input.trim());
console.log(repeat(factorial(N).toString()));
rl.close();
});
function factorial(N) {
if (N === 0) {
return 1;
} else if (N >= 6) {
return 720;
}
return N * factorial(N - 1);
}
function repeat(N) {
if (N.length === 1) {
return N;
} else {
const Arr = N.split('');
let sum = 0;
for (let i = 0; i < Arr.length; i++) {
sum += parseInt(Arr[i]);
}
return repeat(sum.toString());
}
}
댓글남기기