https://www.acmicpc.net/problem/3003
Question
동혁이는 오래된 창고를 뒤지다가 낡은 체스판과 피스를 발견했다.
체스판의 먼지를 털어내고 걸레로 닦으니 그럭저럭 쓸만한 체스판이 되었다. 하지만, 검정색 피스는 모두 있었으나, 흰색 피스는 개수가 올바르지 않았다.
체스는 총 16개의 피스를 사용하며, 킹 1개, 퀸 1개, 룩 2개, 비숍 2개, 나이트 2개, 폰 8개로 구성되어 있다.
동혁이가 발견한 흰색 피스의 개수가 주어졌을 때, 몇 개를 더하거나 빼야 올바른 세트가 되는지 구하는 프로그램을 작성하시오.
Input
첫째 줄에 동혁이가 찾은 흰색 킹, 퀸, 룩, 비숍, 나이트, 폰의 개수가 주어진다. 이 값은 0보다 크거나 같고 10보다 작거나 같은 정수이다.
Output
첫째 줄에 입력에서 주어진 순서대로 몇 개의 피스를 더하거나 빼야 되는지를 출력한다. 만약 수가 양수라면 동혁이는 그 개수 만큼 피스를 더해야 하는 것이고, 음수라면 제거해야 하는 것이다.
Example
example 1
input
0 1 2 2 2 7
output
1 0 0 0 0 1
example 2
input
2 1 2 1 2 1
output
-1 0 0 1 0 7
Problem Solution
고정된 킹, 퀸, 룩, 비숍, 나이트, 폰의 개수와 입력된 각 개수의 차이를 구한다.
Answer 1
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
final int KING_LIMIT = 1;
final int QUEEN_LIMIT = 1;
final int LOOK_LIMIT = 2;
final int BISHOP_LIMIT = 2;
final int KNIGHT_LIMIT = 2;
final int PAWN_LIMIT = 8;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputLine = br.readLine();
StringTokenizer st = new StringTokenizer(inputLine, " ");
int kingCnt = Integer.parseInt(st.nextToken());
int queenCnt = Integer.parseInt(st.nextToken());
int lookCnt = Integer.parseInt(st.nextToken());
int bishopCnt = Integer.parseInt(st.nextToken());
int knightCnt = Integer.parseInt(st.nextToken());
int pawnCnt = Integer.parseInt(st.nextToken());
int kingPieceCalc = KING_LIMIT - kingCnt;
int queenPieceCalc = QUEEN_LIMIT - queenCnt;
int lookPieceCalc = LOOK_LIMIT - lookCnt;
int bishopPieceCalc = BISHOP_LIMIT - bishopCnt;
int knightPieceCalc = KNIGHT_LIMIT - knightCnt;
int pawnPieceCalc = PAWN_LIMIT - pawnCnt;
StringBuilder sb = new StringBuilder();
sb.append(kingPieceCalc);
sb.append(" ");
sb.append(queenPieceCalc);
sb.append(" ");
sb.append(lookPieceCalc);
sb.append(" ");
sb.append(bishopPieceCalc);
sb.append(" ");
sb.append(knightPieceCalc);
sb.append(" ");
sb.append(pawnPieceCalc);
System.out.println(sb);
}
}
Code Explanation
final int KING_LIMIT = 1;
final int QUEEN_LIMIT = 1;
final int LOOK_LIMIT = 2;
final int BISHOP_LIMIT = 2;
final int KNIGHT_LIMIT = 2;
final int PAWN_LIMIT = 8;
체스에서 킹, 퀸, 룩, 비숍, 나이트, 폰의 개수는 변하지 않기 때문에 final 예약어를 사용하여 상수로써 선언한다.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.in은 사용자로부터 입력을 바이트 단위로 받기 위한 입력 스트림이다.
InputStreamReader 클래스로 바이트 기반 스트림을 문자 기반 스트림으로 연결시켜 준다.
BufferedReader 클래스로 사용자가 입력한 문자 스트림을 읽는다.
String inputLine = br.readLine();
StringTokenizer st = new StringTokenizer(inputLine, " ");
BufferedReader 클래스로 받은 문자열 한 행을 읽어온다.
읽어온 문자열은 StringTokenizer 클래스를 이용하여 공백 기준으로 나눈다.
StringBuffer sb = new StringBuffer();
StringBuffer sb = new StringBuffer();
sb.append(kingPieceCalc);
sb.append(" ");
sb.append(queenPieceCalc);
sb.append(" ");
sb.append(lookPieceCalc);
sb.append(" ");
sb.append(bishopPieceCalc);
sb.append(" ");
sb.append(knightPieceCalc);
sb.append(" ");
sb.append(pawnPieceCalc);
결과를 출력하기 위해 StringBuffer 클래스를 이용하여 문자열를 추가한다.
Answer 2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringJoiner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
final int KING_LIMIT = 1;
final int QUEEN_LIMIT = 1;
final int LOOK_LIMIT = 2;
final int BISHOP_LIMIT = 2;
final int KNIGHT_LIMIT = 2;
final int PAWN_LIMIT = 8;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputLine = br.readLine();
StringTokenizer st = new StringTokenizer(inputLine, " ");
int kingCnt = Integer.parseInt(st.nextToken());
int queenCnt = Integer.parseInt(st.nextToken());
int lookCnt = Integer.parseInt(st.nextToken());
int bishopCnt = Integer.parseInt(st.nextToken());
int knightCnt = Integer.parseInt(st.nextToken());
int pawnCnt = Integer.parseInt(st.nextToken());
int kingPieceCalc = KING_LIMIT - kingCnt;
int queenPieceCalc = QUEEN_LIMIT - queenCnt;
int lookPieceCalc = LOOK_LIMIT - lookCnt;
int bishopPieceCalc = BISHOP_LIMIT - bishopCnt;
int knightPieceCalc = KNIGHT_LIMIT - knightCnt;
int pawnPieceCalc = PAWN_LIMIT - pawnCnt;
StringJoiner sj = new StringJoiner(" ");
sj.add(Integer.toString(kingPieceCalc));
sj.add(Integer.toString(queenPieceCalc));
sj.add(Integer.toString(lookPieceCalc));
sj.add(Integer.toString(bishopPieceCalc));
sj.add(Integer.toString(knightPieceCalc));
sj.add(Integer.toString(pawnPieceCalc));
System.out.println(sj);
}
}
Code Explanation
Anwser 1과 다른 코드만 설명한다.
StringJoiner sj = new StringJoiner(" ");
sj.add(Integer.toString(kingPieceCalc));
sj.add(Integer.toString(queenPieceCalc));
sj.add(Integer.toString(lookPieceCalc));
sj.add(Integer.toString(bishopPieceCalc));
sj.add(Integer.toString(knightPieceCalc));
sj.add(Integer.toString(pawnPieceCalc));
문자열 중간에 공백이 있어야 하는데 StringBuffer 클래스를 사용하여 문자열을 합치는 방법보다 StringJoiner 클래스로 문자열을 합치는 방법에서 가독성이 더 좋다.
Organize
이 문제에서 StringBuilder 클래스와 StringJoiner의 성능은 큰 차이가 없어보인다.
처음 사용해보는 StringJoiner 클래스에 대해 알아보고 다음 게시물로 올려야겠다.
문자열 연결
2023.10.24 - [Baekjoon(JAVA)/Study] - 문자열 연결 (StringJoiner, String.join())