2020 카카오 인턴십 - 키패드 누르기 - JAVA

2025. 2. 24. 15:38백준 및 프로그래머스/프로그래머스 LV 1

문제 설명

 

 

문제 요약
1.처음 왼손과 오른손은 *, #에서 시작합니다.
2.1,4,7은 왼손으로 입력을 3,6,9,는 오른손으로 입력합니다.
3. 2,5,8,0 현재 거리를 비교합니다.
EX) 예를 들어 왼손 위치가 1,0이고 오른손 위치가 0,2라고 가정을 해봅니다. 5는 1,1이기 때문에 거리상으로는 왼손 위치가 5까지의 거리가 더 가깝습니다. 그럼 왼손으로 입력 후 왼손 위치를 1,1로 저장해줍니다.

4.혹시 2,5,8,0의 위치가 왼손,오른손 거리가 같을 경우 사용자가 왼손잡이라면 왼손, 오른손 잡이라면 오른손으로 입력해줍니다.

 

import java.lang.*;
class Solution {
    public String solution(int[] numbers, String hand) {
        String[][] number = {
            {"1","2","3"},
            {"4","5","6"},
            {"7","8","9"},
            {"*","0","#"}
        };
        StringBuilder sb = new StringBuilder();
        //왼손 위치
        int lx = 3;
        int ly = 0;
        //오른손 위치
        int rx = 3;
        int ry = 2;
        
        for(int i = 0; i<numbers.length; i++){
            int num = numbers[i];
            //System.out.println(lx+" "+ly);
            //System.out.println(rx+" "+ry);
            if(num == 1 || num == 4 || num == 7){
                sb.append("L");
                if(num == 1){
                    lx = 0;
                    ly = 0;
                }else if(num == 4){
                    lx = 1;
                    ly = 0;
                }else if(num == 7){
                    lx = 2;
                    ly = 0;
                }
            }else if(num == 3 || num == 6 || num == 9){
                sb.append("R");
                if(num == 3){
                    rx = 0;
                    ry = 2;
                }else if(num == 6){
                    rx = 1;
                    ry = 2;
                }else if(num == 9){
                    rx = 2;
                    ry = 2;
                }
            }else{
                //distance1 , distance2를 비교하여 왼손으로 입력할지 오른손으로 입력할지 체크 후 위치 저장.
                if(num == 2){
                    int distace1 = Math.abs(rx-0) + Math.abs(ry-1);
                    int distace2 = Math.abs(lx-0) + Math.abs(ly-1);
                    if(distace1 > distace2){
                        sb.append("L");
                        lx = 0;
                        ly = 1;
                    }else if (distace1 < distace2){
                        sb.append("R");
                        rx = 0;
                        ry = 1;
                    }else{
                        if(hand.equals("right")){
                            sb.append("R");
                            rx = 0;
                            ry = 1;
                        }else{
                            sb.append("L");
                            lx = 0;
                            ly = 1;
                        }
                    }
                }
                if(num == 5){
                    int distace1 = Math.abs(rx-1) + Math.abs(ry-1);
                    int distace2 = Math.abs(lx-1) + Math.abs(ly-1);
                    if(distace1 > distace2){
                        sb.append("L");
                        lx = 1;
                        ly = 1;
                    }else if (distace1 < distace2){
                        sb.append("R");
                        rx = 1;
                        ry = 1;
                    }else{
                        if(hand.equals("right")){
                            sb.append("R");
                            rx = 1;
                            ry = 1;
                        }else{
                            sb.append("L");
                            lx = 1;
                            ly = 1;
                        }
                    }
                }
                if(num == 8){
                    int distace1 = Math.abs(rx-2) + Math.abs(ry-1);
                    int distace2 = Math.abs(lx-2) + Math.abs(ly-1);
                    if(distace1 > distace2){
                        sb.append("L");
                        lx = 2;
                        ly = 1;
                    }else if (distace1 < distace2){
                        sb.append("R");
                        rx = 2;
                        ry = 1;
                    }else{
                        if(hand.equals("right")){
                            sb.append("R");
                            rx = 2;
                            ry = 1;
                        }else{
                            sb.append("L");
                            lx = 2;
                            ly = 1;
                        }
                    }
                }
                if(num == 0){
                    int distace1 = Math.abs(rx-3) + Math.abs(ry-1);
                    int distace2 = Math.abs(lx-3) + Math.abs(ly-1);
                    if(distace1 > distace2){
                        sb.append("L");
                        lx = 3;
                        ly = 1;
                    }else if (distace1 < distace2){
                        sb.append("R");
                        rx = 3;
                        ry = 1;
                    }else{
                        if(hand.equals("right")){
                            sb.append("R");
                            rx = 3;
                            ry = 1;
                        }else{
                            sb.append("L");
                            lx = 3;
                            ly = 1;
                        }
                    }
                }
            }
            //System.out.println(lx+" "+ly);
            //System.out.println(rx+" "+ry);
            //System.out.println("=======================");
           
        }
        return sb.toString();
    }
}

 

요약: 문제는 구현 문제이지만, 요구 사항에 잘 맞게 구현하는 것이 핵심이었습니다. 조금 더 효율적으로 작성할 수 있는 코드라 생각합니다.