[백준 코딩테스트] 30802번 웰컴 키트

gyeol·2024년 8월 5일

코딩테스트 공부

목록 보기
25/53
post-thumbnail

풀이

처음엔 사이즈별 인원 수를 셀 때

for(int j : size){
            if(j <= t) cnt++;
            else{
                while(j > t){
                    j-=t;
                    cnt++;
                }
                cnt++;
            }
        }

이렇게 적었었다. 근데 이렇게 되면 t가 1이거나 사이즈별 인원이 0명일 때도 cnt수가 카운트 되기에 테스트에 실패를 했었다.
반례 상황들을 고려해 코드를 수정했더니 통과할 수 있었다.

내 코드

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt(); //전체 인원
        int[] size = new int[6];

        for(int i=0; i<6; i++){
            size[i] = in.nextInt(); //티셔츠 사이즈 별 인원
        }

        int t = in.nextInt(); //티셔츠 묶음
        int p = in.nextInt(); //펜 묶음

        int cnt=0;

        for(int j : size){
            cnt += j/t;
            if(j!=0 && t!=1 && j%t !=0){
                cnt++;
            }
        }

        System.out.println(cnt);
        System.out.println(n/p + " " + n%p);

    
    }
}
profile
공부 기록 공간 '◡'

0개의 댓글