백준 알고리즘

[백준 2455 : 지능형기차] Java, 시뮬레이션

BallPyhton 2019. 1. 6. 21:20
백준 2455번 문제 풀이입니다.

4개의 역에서 내린 사람 수와 탄 사람 수가 주어졌을 때, 기차에
승객이 가장 많을 때, 그 사람 수를 계산하는 프로그램입니다.

정답률도 높고 간단한 문제입니다!

1. 각 역마다 내리는 사람 수와 타는 사람 수를 입력받습니다. (변수 out, in)
2. 그 역마다 승객 수를 구해줍니다. 
3. max 함수를 이용해서 최대 승객 값을 max_total에 넣고 출력해줍니다.



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
29
30
31
32
package mnth_12;
import java.util.Scanner;
 
 
public class BJ2455 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int out = 0,in = 0;
        int total = 0,max_total = 0;
        for(int i=0;i<4;i++) {
            out = scan.nextInt();
            in = scan.nextInt();
            total = total-out+in;
            max_total = max(max_total,total);
        }
        System.out.println(max_total);
    }
 
    private static int max(int total, int i) {
        // TODO Auto-generated method stub
        if(total>=i)
            return total;
        else
            return i;
    }
 
}
 
cs