문제
https://www.acmicpc.net/problem/3003
체스 세트에서 빠진 말의 개수를 구하는 문제였다.
문제 풀이
원래 체스의 개수에서 내가 발견한 체스의 수를 빼면, 몇 개의 체스가 더 필요하거나 없어져야 하는지 알 수 있다. 따라서 아래와 같은 코드를 생각했다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader rd= new BufferedReader(new InputStreamReader(System.in));
//원래 체스의 수
int []array= {1,1,2,2,2,8};
//내가 찾은 체스의 수
StringTokenizer t = new StringTokenizer(rd.readLine(), " ");
if(t.hasMoreTokens()) {
for(int i=0; i<array.length; i++) {
//내가 찾은 체스의 수-원래 체스의 수
System.out.print((array[i]-Integer.parseInt(t.nextToken()))+" ");
}
}
}
}
다음과 같이 내가 찾은 체스의 수도 배열에 저장하는 방법도 있다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(rd.readLine(), " ");
int[] originalPieces = {1, 1, 2, 2, 2, 8};
//내가 찾은 체스의 개수를 저장할 배열
int[] inputPieces = new int[6];
for (int i = 0; i < 6; i++) {
inputPieces[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < 6; i++) {
System.out.print((originalPieces[i] - inputPieces[i]) + " ");
}
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준/JAVA]10988번 팰린드롬인지 확인하기 (0) | 2025.01.21 |
---|---|
[백준/JAVA]2444번 별 찍기 - 7 (0) | 2025.01.21 |
[백준/JAVA]25083번 새싹🌱 (0) | 2025.01.20 |
[백준/JAVA]11718번 그대로 출력하기 (0) | 2025.01.20 |
[백준/JAVA]5622번 다이얼 (0) | 2025.01.17 |