[알고리즘] 이코테 - 구현

서지혜·2023년 6월 12일

TIL

목록 보기
8/22

이것이 취업을 위한 코딩 테스트다 [PART02 구현] 문제 풀이

1. 상하좌우 (예제 4-1)

N by N 공간의 크기 값인 N과 방향 L, R, U, D가 주어질 때 여행가 A가 마지막에 위치하는 좌표를 구한다. N이상, 1이하의 좌표로 이동해야 하는 경우는 무시한다.

🍵 코드

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
        
        Scanner sc = new Scanner(System.in);
        
        int N = Integer.parseInt(sc.nextLine());
        String[] dir = sc.nextLine().split(" ");
        
        int x = 1;
        int y = 1;
        
        for(int i=0; i<dir.length; i++){
            if(dir[i].equals("L") && y-1 >= 1)
                y--;
            else if(dir[i].equals("R") && y+1 <= N)
                y++;
            else if(dir[i].equals("U") && x-1 >= 1)
                x--;
            else if(dir[i].equals("D") && x+1 <= N)
                x++;
        }

      System.out.println(x + " " + y);
    }
}

2. 시각 (예제 4-2)

00시 00분 00초 ~ N시 59분 59초 까지의 모든 시각에서 3이 한 번이라도 포함된 모든 시각의 경우의 수를 센다. for문을 3중으로 돌면서 시, 분, 초의 값을 문자열 형식으로 바꾸어 3이 포함되었는지 검사한다.

🍵 코드

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
        
        Scanner sc = new Scanner(System.in);
        
        int N = Integer.parseInt(sc.nextLine());
        int count = 0;
        
        for(int i=0; i<N+1; i++){
            for(int j=0; j<60; j++){
                for(int k=0; k<60; k++){
                    if((i+" "+j+" "+k).indexOf("3") != -1){
                        count++;
                    }
                    
                }
            }
        }
        
      System.out.println(count);
    }
}

3. 왕실의 나이트 (실전문제 1)

체스판의 구간을 아래와 같이 5개로 나누어 문제를 해결하였다.

🍵 코드

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
      
        Scanner sc = new Scanner(System.in);
      
        String input = sc.nextLine();
     
        int x = input.charAt(0);
        int y = input.charAt(1);
        
        int result = 0;
        
        if(x >= 'c' && x <= 'f' && y >= '3' && y <= '6')
            result = 8;
        else if((x == 'a' || x == 'h') && (y == '1' || y == '8'))
            result = 2;
        else if(((x == 'a' || x == 'h') && (y == '2' || y == '7'))||((x == 'b' || x == 'g') && (y == '1' || y == '8')))
            result = 3;
        else if(((x == 'a' || x == 'h') && (y >= '3' || y <= '6'))||((x >= 'c' || x <= 'f') && (y == '1' || y == '8')) || ((x == 'b' || x == 'g') && (y == '2' || y == '7')))
            result = 4;
        else
            result = 6;
      
        System.out.println(result);
    }
}

4. 게임 개발 (실전문제 2)

import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
      
        Scanner sc = new Scanner(System.in);
      
        int[] nm = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int[] ABd = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
      
        ArrayList<Integer[]> map = new ArrayList<>();
      
        for(int i=0; i<nm[0]; i++){
             map.add(Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();)
        }
            
        int[] dirList = [0, 1, 2, 3];    
        int[] dirMovingX = []
        
        int nowDir = ABd[2];
        
        boolean moving = true;
        
        while(moving){
            int nextDir = dirList[(nowDir+1)%4]
            
            
            
            
        }
        
        
   
    }
}

profile
개발자가 되고 싶은 감자

0개의 댓글