-
[백준 단계별로 풀어보기] 7. 문자열 (feat. NodeJS)Development/Program Solving 2020. 12. 29. 03:08728x90
11654번: 아스키 코드
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const result = []; for (let i = 0; i < 26; i++) { result[i] = inputValue.indexOf(String.fromCharCode(i + 97)); } console.log(result.join(" ")); process.exit(); }; rl.on("line", onInput).on("close", onClose);
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { console.log(inputValue.charCodeAt()); process.exit(); }; rl.on("line", onInput).on("close", onClose);
11720번: 숫자의 합
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const array = []; const onInput = (input) => array.push(input); const onClose = () => { const numbers = array[1].split(""); const total = numbers.reduce((pre, cur) => pre + Number(cur), 0); console.log(total); process.exit(); }; rl.on("line", onInput).on("close", onClose);
10809번: 알파벳 찾기
CASE 1
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const baseArray = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ]; const alphabetArray = inputValue.split(""); baseArray.map((value, index) => { const foundIndex = alphabetArray.indexOf(value); baseArray[index] = foundIndex; }); console.log(baseArray.join(" ")); process.exit(); }; rl.on("line", onInput).on("close", onClose);
CASE 2
다른 분이 푸신거 참고해서 더 깔끔해서 변경했다.
아스키 코드를 사용해서 풀다니 메모메모✍️
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const result = []; for (let i = 0; i < 26; i++) { result[i] = inputValue.indexOf(String.fromCharCode(i + 97)); } console.log(result.join(" ")); process.exit(); }; rl.on("line", onInput).on("close", onClose);
2675번: 문자열 반복
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const array = []; const onInput = (input) => array.push(input); const onClose = () => { let result = ""; for (let i = 1; i <= array.length - 1; i++) { const [count, strValue] = array[i].split(" "); for (let x = 0; x < strValue.length; x++) { result += strValue[x].repeat(count); } console.log(result); result = ""; } process.exit(); }; rl.on("line", onInput).on("close", onClose);
1157번: 단어 공부
CASE 1
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value.toLowerCase()); const onClose = () => { const obj = {}; let answerArray = []; let count = 0; let searchNum = -1; const charArray = inputValue.split("").filter((value, index) => inputValue.indexOf(value) === index); for (const charValue of charArray) { while (true) { searchNum = inputValue.indexOf(charValue, searchNum + 1); if (searchNum === -1) { break; } count++; } obj[charValue] = count; count = 0; searchNum = -1; } let arr = Object.values(obj); let max = Math.max(...arr); for (const key in obj) { const value = obj[key]; if (value === max) { answerArray.push(key); } } if (answerArray.length > 1) { console.log("?"); } else { console.log(answerArray[0].toUpperCase()); } process.exit(); }; rl.on("line", onInput).on("close", onClose);
CASE 2
문자 관련된건 아스키코드로 푸는게 더 간편한 것 같다.
응용을 잘하자!!!!!!
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value.toLowerCase()); const onClose = () => { let maxIndex = -1; let resultArray = []; const alphabetArray = new Array(26).fill(0); for (let i = 0; i < inputValue.length; i++) { alphabetArray[inputValue.charCodeAt(i) - 97]++; } const max = Math.max(...alphabetArray); while (1) { maxIndex = alphabetArray.indexOf(max, maxIndex + 1); if (maxIndex === -1) { break; } resultArray.push(maxIndex); } if (resultArray.length > 1) { console.log("?"); } else { console.log(String.fromCharCode((resultArray[0] + 97).toString()).toUpperCase()); } process.exit(); }; rl.on("line", onInput).on("close", onClose);
1152번: 단어의 개수
너무 쉬운 문제인데 왜 합격율을 23% 밖에 안되지? 라고 생각했다가 된통 망한 문제다.
함정이 숨어있는데 문자열이 공백뿐일수도 있다.
따라서, 값이 공백일 때 예외처리를 해줘야한다.
문제를 제대로 읽고 예외처리에 대해서도 생각하자!
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value.trimStart().trimEnd()); const onClose = () => { const result = inputValue.split(" "); if (!result[0]) { console.log(0); } else { console.log(result.length); } process.exit(); }; rl.on("line", onInput).on("close", onClose);
2908번: 상수
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const reverse = (string) => { return Number([...string].reverse().join("")); }; const onClose = () => { const array = inputValue.split(" "); const max = Math.max(reverse(array[0]), reverse(array[1])); console.log(max); process.exit(); }; rl.on("line", onInput).on("close", onClose);
5622번: 다이얼
CASE 1
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const phone = [ ["A", "B", "C"], ["D", "E", "F"], ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ]; let answer = []; for (const value of inputValue) { for (const index in phone) { const number = phone[index].indexOf(value); if (number > -1) { answer.push(index); } } } const sum = answer.reduce((pre, cur) => pre + (Number(cur) + 3), 0); console.log(sum); process.exit(); }; rl.on("line", onInput).on("close", onClose);
CASE 2
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const result = inputValue.split("").reduce((pre, cur) => { const ascii = cur.charCodeAt(); if (ascii >= 65 && ascii <= 67) { return pre + 3; } else if (ascii >= 68 && ascii <= 70) { return pre + 4; } else if (ascii >= 71 && ascii <= 73) { return pre + 5; } else if (ascii >= 74 && ascii <= 76) { return pre + 6; } else if (ascii >= 77 && ascii <= 79) { return pre + 7; } else if (ascii >= 80 && ascii <= 83) { return pre + 8; } else if (ascii >= 84 && ascii <= 86) { return pre + 9; } else if (ascii >= 87 && ascii <= 90) { return pre + 10; } }, 0); console.log(result); process.exit(); }; rl.on("line", onInput).on("close", onClose);
2941번: 크로아티아 알파벳
CASE 1
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const croatias = ["c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="]; let count = 0; for (const croatia of croatias) { while (1) { const index = inputValue.indexOf(croatia); if (index === -1) { break; } count++; inputValue = inputValue.slice(0, index) + " " + inputValue.slice(index + croatia.length); } } count += inputValue.replace(/([^a-z])/g, "").length; console.log(count); process.exit(); }; rl.on("line", onInput).on("close", onClose);
CASE 2
정규표현식을 잘 활용하면 이렇게 간단하게 완성할 수 있다.
정규표현식 공부해야지..🤦♀️
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let inputValue; const onInput = (value) => (inputValue = value); const onClose = () => { const result = inputValue.replace(/c\=|c\-|dz\=|d\-|lj|nj|s\=|z\=/g, " "); console.log(result.length); process.exit(); }; rl.on("line", onInput).on("close", onClose);
🚨 1316번: 그룹 단어 체커
이 문제는 처음으로 혼자 못 푼 문제.
나중에 다시 풀어보기.
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const array = []; const onInput = (input) => array.push(input); const onClose = () => { let countGroupWord = 0; for (let i = 1; i < array.length; i++) { const word = array[i]; const firstLetters = []; let isGroupWord = true; for (const letter of word) { if (firstLetters.indexOf(letter) === -1) { firstLetters.push(letter); } else { if (firstLetters.indexOf(letter) !== firstLetters.length - 1) { isGroupWord = false; break; } } } if (isGroupWord) { countGroupWord++; } } console.log(countGroupWord); process.exit(); }; rl.on("line", onInput).on("close", onClose);
728x90'Development > Program Solving' 카테고리의 다른 글
[백준 2292번] 벌집 (feat. NodeJS) (0) 2021.01.06 [백준 1712번] 손익분기점 (feat. NodeJS) (0) 2021.01.06 [백준 단계별로 풀어보기] 6. 함수 (feat. NodeJS) (0) 2020.12.27 [백준 단계별로 풀어보기] 3. for문 (feat.NodeJS) (0) 2020.12.25 [백준 단계별로 풀어보기] 5. 1차원 배열 (feat.NodeJS) (0) 2020.12.25