210920 - 이코테/예제 4-1

letsbebrave·2021년 9월 20일
0

codingtest

목록 보기
3/146
post-thumbnail
  1. 문자열을 공백을 기준으로 어떻게 자르는지 몰라서 순열을 이용해서 짝수번째에 있는 문자들만 orderArr이라는 char배열에 넣어줬다.
package september_algorithm;
import java.util.*;

public class september20 {

	public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        
        int num = sc.nextInt();
        
        sc.nextLine();
        
        String order = sc.nextLine(); // 줄 단위로 입력받음 (next()는 공백문자까지 입력받음)
        char[] orderChar = order.toCharArray();
        
        System.out.println(order.length());
        
        int realOrder = 0;
        realOrder = order.length() - order.length()/2;
        
        char[] orderArr = new char[realOrder];
        
        int y = 0; 
        
        for (int i = 0; i < order.length(); i++){
            if (i%2 != 1){
            	orderArr[y] += orderChar[i];
            	y += 1;
            }
            
        }
      
            System.out.println(Arrays.toString(orderArr) + "\n");
            
            //입력값
            5
            R R R U D D
            //출력값
            11
            [R, R, R, U, D, D]

	}
}

코드

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // N을 입력받기
        int n = sc.nextInt();
        sc.nextLine(); // 버퍼 비우기
        String[] plans = sc.nextLine().split(" ");
        int x = 1, y = 1;

        // L, R, U, D에 따른 이동 방향 
        int[] dx = {0, 0, -1, 1};
        int[] dy = {-1, 1, 0, 0};
        char[] moveTypes = {'L', 'R', 'U', 'D'};

        // 이동 계획을 하나씩 확인
        for (int i = 0; i < plans.length; i++) {
            char plan = plans[i].charAt(0);
            // 이동 후 좌표 구하기 
            int nx = -1, ny = -1;
            for (int j = 0; j < 4; j++) {
                if (plan == moveTypes[j]) {
                    nx = x + dx[j];
                    ny = y + dy[j];
                }
            }
            // 공간을 벗어나는 경우 무시 
            if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
            // 이동 수행 
            x = nx;
            y = ny;
        }

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

}
profile
그게, 할 수 있다고 믿어야 해

0개의 댓글