본문 바로가기

Coding/백준

1541번 잃어버린 괄호

 '-'가 나올때까지 더하다가 '-'가 나온 순간부터 값들을 모조리 빼주면 된다.

 

 

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
33
34
35
36
37
#include <iostream>
#include <vector>
using namespace std;
 
string str;
string num;
int ans;
bool plmi=true;
 
void solved(){
    for(int i=0;i<str.size()+1;i++){
        if(str[i]=='+' || str[i]=='-' || str[i]=='\0'){
            if(plmi){
                ans+=stoi(num);
            }
            else{
                ans-=stoi(num);
            }
            if(str[i]=='-'){
                plmi=false;
            }
            num="";
        }
        else{
            num+=str[i];
        }
    }
}
 
int main(){
    cin >> str;
    
    solved();
    
    cout << ans << endl;
}
 
cs

'Coding > 백준' 카테고리의 다른 글

2252번 줄 세우기  (0) 2021.03.02
9251번 LCS  (0) 2021.02.06
2798번 블랙잭  (0) 2021.01.20
6603번 로또  (0) 2021.01.20
4963번 섬의 개수  (0) 2021.01.13