Algorithm

백준 10810 자바 공넣기

hyunjun's developing 🏣 2024. 1. 3. 21:37

 

 

https://www.acmicpc.net/problem/10810

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner;
 
public class BasketSimulation {
 
    public static void main(String[] args) {
 
        Scanner scanner = new Scanner(System.in);
 
        int basketCount = scanner.nextInt();
        int[] basketArray = new int[basketCount];
        int operationCount = scanner.nextInt();
 
        for(int i = 0; i < operationCount; i++) {
            int startBasket = scanner.nextInt();
            int endBasket = scanner.nextInt();
            int ballNumber = scanner.nextInt();
 
            for(int j = startBasket - 1; j < endBasket; j++) {
                basketArray[j] = ballNumber;
            }
        }
 
        for(int k = 0; k < basketArray.length; k++) {
            System.out.print(basketArray[k] + " ");
        }
    }
}
 
cs

 

먼저 스캐너를 선언해준다. 그 다음 바구니의 갯수를 스캐너를 통해 입력 받고 배열 basketArray를 바구니의 개수만큼 만들어준다. 그 다음 공을 던질 횟수인 operationCount를 스캐너를 통해 입력받는다. 그 다음 이 행동 횟수만큼 바깥 for문을 작동시킨다. startBasket부터 endBasket까지 ballnumber가 적혀진 공을 넣어야 한다. 안쪽 for문(두 번째)이 그 역할을 수행한다. 그런 다음 배열의 길이만큼 배열을 출력하면 모든 배열이 나오니 그렇게 출력을 하면 된다.  변수를 백준 형식에 맞춰서 실행시키면 잘 될 것이다.