이것이 취업을 위한 코딩 테스트다 [PART02 구현] 문제 풀이
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);
}
}
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);
}
}
체스판의 구간을 아래와 같이 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);
}
}
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]
}
}
}