✏️ 문제풀이/백준

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

bono-hye 2024. 4. 15. 23:41

🌱 Scanner 사용

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		int king = 1;
		int queen = 1;
		int rook = 2;
		int bishop = 2;
		int knight = 2;
		int pawn = 8;
		
		int scKing = king - sc.nextInt();
		int scQueen = queen - sc.nextInt();
		int scRook = rook - sc.nextInt();
		int scBishop = bishop - sc.nextInt();
		int scKnight = knight - sc.nextInt();
		int scPawn = pawn - sc.nextInt();
		
		System.out.print(scKing + " ");
		System.out.print(scQueen + " ");
		System.out.print(scRook + " ");
		System.out.print(scBishop + " ");
		System.out.print(scKnight + " ");
		System.out.print(scPawn);
        
       	sc.close;
	}
}

 

🌱 Scanner & 배열 사용

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		int[] chess = {1,1,2,2,2,8};
		
		for(int i=0; i<chess.length; i++)
		{
			int n = sc.nextInt();
			System.out.print(chess[i]-n + " ");
		}
		
		sc.close();
	}
}

 

🌱 BufferedReader & 배열 사용

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main
{
	public static void main(String[] args) throws NumberFormatException, IOException 
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		StringTokenizer st = new StringTokenizer(str, " ");
		
		int[] chess = {1,1,2,2,2,8};
		
		for(int i=0; i<chess.length; i++)
			System.out.print(chess[i]-Integer.parseInt(st.nextToken()) + " ");
	}
}

 

💡 정리

각 변수를 선언해서 풀이했지만 배열을 사용하면 더 간단히 해결할 수 있을 것 같아서 배열로도 풀이해봤다!