본문 바로가기
ALGORITHM/Implement

[JAVA] 백준 2455번

by sjs_2215 2019. 8. 25.

문제: https://www.acmicpc.net/problem/2455


2455번 지능형 기차


  • 문제 재정의:

빈칸을 두고 입력됨

모두 내려야 승차 가능

기차에 제일 많은 사람이 있을 때를 출력

총 4개의 역. 역 번호 순서대로 운행



  • 생각한 것:
  1. 사람 수 어떤 자료구조에 저장

배열? 4개의 역이라 2차원 배열 말고 그냥 8개 변수 생성

  1. 현재 남아 있는 승객 수 저장. 더 크면 update



  • 코드:

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        int temp=0;

        st = new StringTokenizer(br.readLine()); //공백 기준으로 나눔
          int one_in = Integer.parseInt(st.nextToken()); //1역 in. 0
           int one_out = Integer.parseInt(st.nextToken()); //1역 out

           temp=current_num(0,one_in, one_out);

        st = new StringTokenizer(br.readLine()); //공백 기준으로 나눔
          int two_in = Integer.parseInt(st.nextToken()); //2역 in
           int two_out = Integer.parseInt(st.nextToken()); //2역 out

           if(temp<current_num(temp,two_in, two_out)){
               temp=current_num(temp,two_in, two_out);
           }


        st = new StringTokenizer(br.readLine()); //공백 기준으로 나눔
          int three_in = Integer.parseInt(st.nextToken()); //3 in
           int three_out = Integer.parseInt(st.nextToken()); //3역 out

           if(temp<current_num(temp,three_in, three_out)){
               temp=current_num(temp,three_in, three_out);
           }


        st = new StringTokenizer(br.readLine()); //공백 기준으로 나눔
          int four_in = Integer.parseInt(st.nextToken()); //4역 in
           int four_out = Integer.parseInt(st.nextToken()); //4역 out. 0 

           if(temp<current_num(temp,four_in, four_out)){
               temp=current_num(temp,four_in, four_out);
           }

           System.out.println(temp);

    }
    public static int current_num(int added, int a, int b){
        int now=added;
        now+=b-a;
        return now;
    }


}

'ALGORITHM > Implement' 카테고리의 다른 글

[JAVA] 백준 1789번  (0) 2019.09.01
[JAVA] 백준 10871번  (0) 2019.09.01
[JAVA] 백준 1173번  (0) 2019.08.17
[JAVA] 백준 1592번  (0) 2019.08.17
[JAVA] 프로그래머스 가장 큰 수  (0) 2019.07.15

Comments