[Java] 백준 1541 잃어버린 괄호

Lee GaEun·2025년 1월 27일

[Java] 알고리즘

목록 보기
52/93

1541 잃어버린 괄호 문제 링크

문제


#1

import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String[] plusNum = st.nextToken().split("-");
        System.out.println(doPlus(plusNum));

        bw.flush();
        bw.close();
    }
    static int doPlus(String[] plusNum) {
        int answer = 0;
        for(int i=0; i<plusNum.length; i++) {
            String[] intNum = plusNum[i].split("\\+");
            int a = 0;
            for(int j=0; j<intNum.length; j++) {
                a += Integer.parseInt(intNum[j]);
            }
            if(i==0) answer = a;
            else answer -= a;
        }
        return answer;
    }
}

  • "-"가 나오는 곳부터 다음 "-"가 나오는 곳부터 괄호를 넣으면 최소값
  • 성공!
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글