백준 Laptop Sticker

KIMYEONGJUN·2025년 1월 31일
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

The single line of input contains four integers w_c, h_c, w_s and h_s (1 <= w_c, h_c, w_s, h_s <= 1,000), where w_c is the width of your new laptop computer, h_c is the height of your new laptop computer, w_s is the width of the laptop sticker, and h_s is the height of the laptop sticker.
All measurements are in centimeters.

Output a single integer, which is 1 if the laptop sticker will fit on your new laptop computer, without rotating, but with one centimeter space on all sides, and 0 if the laptop sticker won’t fit.

내가 이 문제를 보고 생각해본 부분

BufferedReader를 사용하여 입력에서 한 줄을 읽고, StringTokenizer를 통해 공백으로 구분된 문자열을 개별 토큰으로 나눠준다.
nextToken() 메서드를 사용하여 각각의 값을 읽고, Integer.parseInt()를 통해 문자열을 정수로 변환하여 변수에 저장한다.
w_c, h_c: 노트북의 너비와 높이
w_s, h_s: 스티커의 너비와 높이
여유 공간 계산:
스티커가 들어갈 수 있는 최대 너비와 높이를 계산한다.
각 방향에서 1cm의 여유 공간을 두기 때문에 2를 빼준다.
조건 확인 및 출력:
스티커의 크기가 여유 공간 내에 들어가는지를 확인한다.
만약 맞다면 1을 출력하고, 그렇지 않으면 0을 출력한다.

코드로 구현

package baekjoon.baekjoon_26;

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

// 백준 21591번 문제
public class Main918 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        // 입력 받기
        int w_c = Integer.parseInt(st.nextToken());
        int h_c = Integer.parseInt(st.nextToken());
        int w_s = Integer.parseInt(st.nextToken());
        int h_s = Integer.parseInt(st.nextToken());

        // 스티커가 들어갈 수 있는 크기 계산
        int availableWidth = w_c - 2; // 양쪽 1cm 여유
        int availableHeight = h_c - 2; // 위아래 1cm 여유

        // 스티커가 들어갈 수 있는지 확인
        if(w_s <= availableWidth && h_s <= availableHeight) {
            System.out.println(1); // 스티커가 들어간다면 1 출력
        } else {
            System.out.println(0); // 들어가지 않는다면 0 출력
        }

        br.close();
    }
}

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글