- 문자열을 공백을 기준으로 어떻게 자르는지 몰라서 순열을 이용해서 짝수번째에 있는 문자들만 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();
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);
int n = sc.nextInt();
sc.nextLine();
String[] plans = sc.nextLine().split(" ");
int x = 1, y = 1;
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);
}
}