백준

C++ 13305 잃어버린 괄호

안선생 2023. 2. 10. 20:42
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
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
 
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
 
    string s; cin >> s;
    
    string a;
    int sum = 0;
    bool check = 1;
    for (int i = 0; i <= s.length();i++)
    {
        if (s[i] == '-' || s[i] == '+' || i==s.length())
        {    
            if (check)
            {                
                sum += stoi(a);;
                a.clear();
            }
            else
            {
                sum -= stoi(a);
                a.clear();
            }
            if (s[i] == '-')
                check = 0;
        }
        else
        a += s[i];
 
    }
    
 
    cout << sum;
 
    return 0;
 
}
 
 
cs

\

 

https://www.acmicpc.net/problem/1541

 

1541번: 잃어버린 괄호

첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. 그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다

www.acmicpc.net