문제
코테 페이지에서 확인하기!
풀이
효성
1
const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())).on('close', () => { main(inputs); process.exit(); }); })(); function main(inputs) { for(const input of inputs) { let answer = 0; for(let i = 1; i <= 7; i++) { if(i % 2 === 1) { answer += +input[i - 1]; } } for(let i = 1; i <= 7; i++) { if(i % 2 === 0 && +input[i - 1] !== 0) { answer *= +input[i - 1]; } } console.log(answer % 10); } }
2
const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())).on('close', () => { main(inputs); process.exit(); }); })(); function main(inputs) { const [N, cryptogram] = inputs; const cryptogramLength = +N; let answer = ''; for(let i = 0; i < cryptogramLength; i += 2) { const char = cryptogram[i]; const time = +cryptogram[i + 1] * +cryptogram[i + 1]; const charCode = char.charCodeAt(0); const decodeCode = charCode - 97 + time; const decodeChar = String.fromCharCode(decodeCode % 26 + 97); answer += decodeChar; } console.log(answer); }
3
const readline = require('readline'); (async () => { const rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', function (line) { inputs.push( line.trim() ); }).on('close', function () { main(inputs); }); })(); function main(inputs) { const T = inputs.shift(); const regExp = /^[a-zA-Z]*$/; for(let i = 0; i < inputs.length; i += 2) { let answer = ''; const targetStr = inputs[i]; const [type, key] = inputs[i + 1].split(' '); const targetKeys = key.repeat(parseInt(targetStr.length / key.length)) + key.slice(0, targetStr.length % key.length); for(let j = 0; j < targetKeys.length; j++) { if(!regExp.test(targetStr[j])) { answer += targetStr[j]; continue; } const NUMBER = targetStr[j].toUpperCase() === targetStr[j] ? 65 : 97; const targetCharCode = targetStr[j].charCodeAt(0) - NUMBER; const keyCharCode = targetKeys[j].charCodeAt(0) % 26; if(type === 'E') { answer += String.fromCharCode((targetCharCode + keyCharCode) % 26 + NUMBER); } if(type === 'D') { answer += String.fromCharCode((targetCharCode - keyCharCode + 26) % 26 + NUMBER); } } console.log(answer); } }
4
현석
// 1번 // Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); let count = 0 rl.on('line', (line) => { if (count++ === 6) rl.close() solution(line.trim()) }) rl.on('close', () => { process.exit() }) })(); function solution(string) { const digits = string.split('').map(Number) const oddSumUnit = digits .flatMap((digit, index) => index % 2 ? [] : [digit]) .reduce((a,b) => a + b) % 10 const evenMulUnit = digits.flatMap((digit, index) => index % 2 && digit !== 0 ? [digit] : []).reduce((a,b) => ((a % 10) * (b % 10) % 10), 1) const result = (oddSumUnit * evenMulUnit) % 10 console.log(result) }
// 2번 // Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); let count = 0 let length let cipher rl.on('line', (line) => { if (count++ === 2) rl.close() if (count === 1) { length = Number(line.trim()) } else if (count === 2) { cipher = line.trim() solution(cipher) } }) rl.on('close', () => { process.exit(); }) })(); function solution(cipher) { const alphabet = cipher.split('').flatMap((char, index) => index % 2 ? [] : [char]) const actionCount = cipher.split('').flatMap((char, index) => index % 2 ? [(Number(char) ** 2) % 26] : []) const result = alphabet.map((char, index) => { const charCode = (char.charCodeAt() + actionCount[index] - 97) % 26 + 97 return String.fromCharCode(charCode) }).join('') console.log(result) }
// 3번 // Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); let index = 0 let testCount let testCase = [] rl.on('line', (line) => { index++ if (index === 1) { testCount = Number(line.trim()) return } testCase.push(line.trimEnd()) if (index % 2) { solution(testCase) testCase = [] } if (index === testCount * 2 + 1) rl.close() }) rl.on('close', () => { process.exit(); }) })(); function solution([first, second]) { const target = first const [flag, token] = second.split(' ') const isEncryption = flag === 'E' const repeatCount = Math.ceil(target.length / token.length) const validToken = token.repeat(repeatCount).slice(0, target.length) const COUNT_ALPHABET = 26 const code = validToken.split('').map(char => char.charCodeAt() % COUNT_ALPHABET) const isSmallAlphabet = (char) => 'a'.charCodeAt() <= char.charCodeAt() && char.charCodeAt() < 'a'.charCodeAt() + COUNT_ALPHABET const isLargeAlphabet = (char) => 'A'.charCodeAt() <= char.charCodeAt() && char.charCodeAt() < 'A'.charCodeAt() + COUNT_ALPHABET const isAlphabet = (char) => isLargeAlphabet(char) || isSmallAlphabet(char) const encrypt = (targetChar, countShift) => { const startCharCode = isSmallAlphabet(targetChar) ? 'a'.charCodeAt() : 'A'.charCodeAt() const charCode = (targetChar.charCodeAt() - startCharCode + countShift) % COUNT_ALPHABET + startCharCode return String.fromCharCode(charCode) } const decrypt = (targetChar, countShift) => { const startCharCode = isSmallAlphabet(targetChar) ? 'a'.charCodeAt() : 'A'.charCodeAt() const charCode = (targetChar.charCodeAt() - startCharCode + 26 - countShift) % COUNT_ALPHABET + startCharCode return String.fromCharCode(charCode) } const result = target.split('').map((char, index) => { if (isAlphabet(char)) return isEncryption ? encrypt(char, code[index]) : decrypt(char, code[index]) else return char }).join('') console.log(result) }
재영
1번
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line)).on('close', () => { main(inputs); process.exit(); }) })(); function main(p) { for (let s of p) { const sArr = [...s] let total = sArr.filter((v, idx) => !(idx % 2)).reduce((acc, cur) => acc + +cur, 0); const res = sArr.filter((v, idx) => (idx % 2) && +v).reduce((acc, cur) => (acc * cur) % 10, total); console.log(res % 10) } }
2번
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line)).on('close', () => { main(inputs); process.exit(); }); })(); function main(p) { const N = p[0]; const S = p[1]; const alphabets = 'abcdefghijklmnopqrstuvwxyz'; console.log(S.replace(/[a-z0-9]{2,2}/g, ([alphabet, num]) => alphabets[((num ** 2 + alphabets.indexOf(alphabet)) % 26)])) }
3번
외…않돼지…
해답: 알고 보니 구름 IDE 처리 문제였던 듯하다.
구름에서
process.exit()
을 제대로 듣지 않는다.6시간을 헤맸는데… 범인은… 나 자신이 아닐 수도 있다… 😡
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line)).on('close', () => main(inputs) ); })(); function* getInput(ips) { let i = 0; let T = +ips[0]; while (i !== T) { yield [ips[i * 2 + 1], ...ips[i * 2 + 2].split(' ')]; i += 1; } return; } const calc = (now, sft, type) => { const ALPHA_LENGTH = 26; const UPPER_START_CODE = 65; const LOWER_START_CODE = 97; const typeWeight = { E: 1, D: -1, }; const nowWeight = now >= LOWER_START_CODE ? LOWER_START_CODE : UPPER_START_CODE; const resNow = now - nowWeight; const resSft = (ALPHA_LENGTH + typeWeight[type] * (sft % ALPHA_LENGTH)) % ALPHA_LENGTH; return ((ALPHA_LENGTH + resNow + resSft) % ALPHA_LENGTH) + nowWeight; }; function main(ips, exit) { const gen = getInput(ips); for (let [S, type, token] of gen) { const repeatedToken = token.padEnd(S.length, token).slice(0, S.length); const result = S.replace(/[a-zA-Z]/g, (s, idx) => String.fromCharCode( calc(s.charCodeAt(), repeatedToken[idx].charCodeAt(), type) ) ); console.log(result); } }