가지코딩 2025. 4. 4. 12:58

https://school.programmers.co.kr/learn/courses/30/lessons/181926

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


문제

정수 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 함수를 완성해 주세요.

 

제한사항

  • -100,000 ≤ n ≤ 100,000
  • 1 ≤ control의 길이 ≤ 100,000
    • control은 알파벳 소문자 "w", "a", "s", "d"로 이루어진 문자열입니다.

 

입출력 예

n control result
0 "wsdawsdassw" -1

 

입출력 예 #1

  • ncontrol에 따라 다음과 같은 순서로 변하게 됩니다.
  • 0 → 1 → 0 → 10 → 0 → 1 → 0 → 10 → 0 → -1 → -2 → -1
  • 따라서 -1을 return 합니다.

풀이

class Solution {
    public int solution(int n, String control) {
        int answer = n;
        
        for (char c : control.toCharArray()) {
            switch (c) {
                case 'w': answer++; break;
                case 's': answer--; break;
                case 'd': answer+=10; break;
                case 'a': answer-=10; break;
                default: break;
            }
        }
        
        return answer;
    }
}

 

 

 


GitHub: https://github.com/gajicoding/coding_test/edit/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/0/181926.%E2%80%85%EC%88%98%E2%80%85%EC%A1%B0%EC%9E%91%ED%95%98%EA%B8%B0%E2%80%851

 

coding_test/프로그래머스/0/181926. 수 조작하기 1 at main · gajicoding/coding_test

This is an auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - gajicoding/coding_test

github.com


출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

 

코딩테스트 연습 | 프로그래머스 스쿨

개발자 취업의 필수 관문 코딩테스트를 철저하게 연습하고 대비할 수 있는 문제를 총망라! 프로그래머스에서 선발한 문제로 유형을 파악하고 실력을 업그레이드해 보세요!

school.programmers.co.kr