https://school.programmers.co.kr/learn/courses/30/lessons/181926
문제 설명
정수 n과 문자열 control이 주어집니다. control은 "w", "a", "s", "d"의 4개의 문자로 이루어져 있으며, control의 앞에서부터 순서대로 문자에 따라 n의 값을 바꿉니다.
"w" : n이 1 커집니다.
"s" : n이 1 작아집니다.
"d" : n이 10 커집니다.
"a" : n이 10 작아집니다.
위 규칙에 따라 n을 바꿨을 때 가장 마지막에 나오는 n의 값을 return 하는 solution 함수를 완성해 주세요.
🤍 나의 풀이
"w", "a", "s", "d" 라는 조건이 주어지는 상황에서의 문제이기 때문에
case 문으로 풀어보고 싶었다.
function solution(n, control) {
for(let i = 0; i < control.length; i++) {
switch (control[i]) {
case 'w':
n += 1;
break;
case 'a':
n -= 10;
break;
case 's':
n -= 1;
break;
case 'd':
n += 10;
break;
}
}
return n;
}
for문 안에 swich문이 있어서 그런지 시간이 오래걸리는 편
코딩테스트 연습을 해보았다.
눈에 익혀보자 하고 보았는데 잉.. ? 전혀 감이 안잡히는..?
그나마 알고리즘 스터디에서 leetcode 푸는 분 걸 본적이 있어서 입력 출력으로 나오는 건 알겠는데 이게 뭔가 싶었다.
문제 설명
두 정수 a, b가 주어질 때 다음과 같은 형태의 계산식을 출력하는 코드를 작성해 보세요.
a + b = c
제한사항
1 ≤ a, b ≤ 100
입출력 예
입력 #1 ) 4 5
출력 #1 ) 4 + 5 = 9
💁🏻♀️ solution으로 주어지는 식
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
console.log(Number(input[0]) + Number(input[1]));
});
🤍 나의 풀이
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
const a = Number(input[0]) ;
const b = Number(input[1]) ;
const c = a + b ;
console.log (`${a} + ${b} = ${c}`)
});
🎨 다른 사람의 풀이
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
}).on('line', function (line) {
const [a, b] = line.split(' ')
console.log(a, '+', b, '=', Number(a) + Number(b))
})
하루하루 조금씩 풀어나가다보면 실력이 쌓일 것이라고 생각한다.