Algorithm/BOJ

#2460 지능형 기차 2

print("스테코더") 2022. 11. 6. 15:56

지능형 기차 2

2460

문제 풀이 전략

  1. 10개 역의 내린사람(out), 탄 사람(in)을 입력 받음
  2. 기차에 타고 있는 총 인원(total) 계산
    1. total - out
    2. total + in
  3. 한 역을 이동할 때마다 total 비교

코드

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

public class no_2460 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int total = 0;
        int max = -1;
        for (int i = 0; i < 10; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());            
            int out = Integer.parseInt(st.nextToken()); // 내린 사람 수
            int in = Integer.parseInt(st.nextToken()); // 탄 사람 수

            total -= out;
            total += in;
            max = Math.max(total, max);
        }
        
        System.out.println(max);
        br.close();
    }
}