[백준/JAVA]3003번 킹, 퀸, 룩, 비숍, 나이트, 폰

2025. 1. 20. 16:20·코딩테스트/백준

문제

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
'코딩테스트/백준' 카테고리의 다른 글
  • [백준/JAVA]10988번 팰린드롬인지 확인하기
  • [백준/JAVA]2444번 별 찍기 - 7
  • [백준/JAVA]25083번 새싹🌱
  • [백준/JAVA]11718번 그대로 출력하기
yxxjxxeee
yxxjxxeee
  • yxxjxxeee
    공부 일지
    yxxjxxeee
  • 전체
    오늘
    어제
    • study N
      • Framework
        • Spring
      • Language
        • JavaScript
        • C | C++
        • JAVA
        • PHP
      • CS
        • 네트워크
        • 자료구조
        • 데이터베이스
        • 운영체제
      • DBMS
        • MySQL
      • 코딩테스트 N
        • 백준 N
        • 프로그래머스
        • LeetCode
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 쇼핑몰 제작 프로젝트(PHP)
    • github
  • 공지사항

  • 인기 글

  • 최근 글

  • hELLO Designed By 정상우
    Version v4.10.2
yxxjxxeee
[백준/JAVA]3003번 킹, 퀸, 룩, 비숍, 나이트, 폰
상단으로

티스토리툴바