반응형

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

 

19591번: 독특한 계산기

숫자, '+', '*', '-', '/'로만 이루어진 길이가 106 이하인 수식이 주어진다. 계산 과정 중의 모든 수는 −263 이상 263 미만이며, 0으로 나누는 경우는 없다. 숫자 앞에 불필요한 0이 있을 수 있다. 

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 문자열을 입력받을 str 변수를 선언하고, 입력을 받습니다.

 

2. num을 선언하고, 기호가 아닌 문자가 나오면 num = num * 10 + str[ i ] - '0'을 통해 num을 갱신해 줍니다.

 

3. 기호가 나온다면 deque<char> symbol에 기호를 push_back 해주고, 첫 번째 문자열이 '-'라면 isFirst를 true로 바꾸고, num의 값에 -1을 곱해준 후 deque<ll> number에 num을 push_back 해줍니다.

 

4. 이후 규칙에 맞게 계산을 하고, 답을 출력합니다.

 

[ 소스 코드 ]

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<iostream>
#include<deque>
#include<string>
#define ll long long
 
using namespace std;
deque<ll> number;
deque<char> symbol;
ll ans;
 
ll cal(ll num1, ll num2, char sym)
{
    if (sym == '+') {
        return num1 + num2;
    }
    else if (sym == '-') {
        return num1 - num2;
    }
    else if (sym == '*') {
        return num1 * num2;
    }
    else {
        return num1 / num2;
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    string str;
 
    cin >> str;
 
    bool isFirst = false;
    ll num = 0;
 
    for (int i = 0; i < str.size(); i++) {
        if (i == 0 && str[i] == '-') {
            isFirst = true;
        }
        else if (str[i] >= '0') {
            num = num * 10 + str[i] - '0';
        }
        else {
            if (isFirst) {
                isFirst = false;
                num *= -1;
            }
            number.push_back(num);
            symbol.push_back(str[i]);
            num = 0;
        }
    }
    if (isFirst) {
        isFirst = false;
        num *= -1;
    }
    number.push_back(num);
 
    if (number.size() == 1) {
        cout << number.front();
        return 0;
    }
 
    while (1) {
        ll tmp[4= { 0 };
 
        if (number.size() == 3) {
            tmp[0= number.front();
            tmp[3= number.back();
            number.pop_back();
            number.pop_front();
            tmp[1= tmp[2= number.front();
        }
        else if (number.size() == 2) {
            tmp[0= number.front();
            tmp[1= number.back();
        }
        else {
            tmp[0= number.front();
            tmp[3= number.back();
            number.pop_back();
            number.pop_front();
            tmp[1= number.front();
            tmp[2= number.back();
        }
 
        if (symbol.size() == 1) {
            char sym = symbol.front();
            cout << cal(tmp[0], tmp[1], sym);
 
            return 0;
        }
        else {
            char sym1 = symbol.front();
            char sym2 = symbol.back();
 
            if (sym1 == '+' || sym1 == '-') {
                if (sym2 == '*' || sym2 == '/') {
                    symbol.pop_back();
                    number.pop_back();
                    number.push_back(cal(tmp[2], tmp[3], sym2));
                    number.push_front(tmp[0]);
                }
                else {
                    ll num1 = cal(tmp[0], tmp[1], sym1);
                    ll num2 = cal(tmp[2], tmp[3], sym2);
 
                    if (num1 >= num2) {
                        symbol.pop_front();
                        number.pop_front();
                        number.push_front(cal(tmp[0], tmp[1], sym1));
                        number.push_back(tmp[3]);
                    }
                    else {
                        symbol.pop_back();
                        number.pop_back();
                        number.push_back(cal(tmp[2], tmp[3], sym2));
                        number.push_front(tmp[0]);
                    }
                }
            }
            else if (sym1 == '*' || sym1 == '/') {
                if (sym2 == '+' || sym2 == '-') {
                    symbol.pop_front();
                    number.pop_front();
                    number.push_front(cal(tmp[0], tmp[1], sym1));
                    number.push_back(tmp[3]);
                }
                else {
                    ll num1 = cal(tmp[0], tmp[1], sym1);
                    ll num2 = cal(tmp[2], tmp[3], sym2);
 
                    if (num1 >= num2) {
                        symbol.pop_front();
                        number.pop_front();
                        number.push_front(cal(tmp[0], tmp[1], sym1));
                        number.push_back(tmp[3]);
                    }
                    else {
                        symbol.pop_back();
                        number.pop_back();
                        number.push_back(cal(tmp[2], tmp[3], sym2));
                        number.push_front(tmp[0]);
                    }
                }
            }
        }
    }
}
cs
반응형
반응형

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

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

 

 

[ 문제풀이 ]

 

1. deque 자료구조를 이용하여 입력받는 수와 동일한 메서드를 이용하여 조건에 맞는 동작을 수행합니다.

 

[ 소스코드 ]

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<deque>
#include<string>
 
using namespace std;
 
int N;
deque<int> deq;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    for (int i = 0; i < N; i++) {
        string str;
        int num;
 
        cin >> str;
 
        if (str == "push_front") {
            cin >> num;
            deq.push_front(num);
        }
        else if (str == "push_back") {
            cin >> num;
            deq.push_back(num);
        }
        else if (str == "pop_front") {
            if (!deq.empty()) {
                cout << deq.front() << '\n';
                deq.pop_front();
            }
            else {
                cout << -1 << '\n';
            }
        }
        else if (str == "pop_back") {
            if (!deq.empty()) {
                cout << deq.back() << '\n';
                deq.pop_back();
            }
            else {
                cout << -1 << '\n';
            }
        }
        else if (str == "size") {
            cout << deq.size() << '\n';
        }
        else if (str == "empty") {
            if (!deq.empty()) {
                cout << 0 << '\n';
            }
            else {
                cout << 1 << '\n';
            }
        }
        else if (str == "front") {
            if (!deq.empty()) {
                cout << deq.front() << '\n';
            }
            else {
                cout << -1 << '\n';
            }
        }
        else if (str == "back") {
            if (!deq.empty()) {
                cout << deq.back() << '\n';
            }
            else {
                cout << -1 << '\n';
            }
        }
    }
}
cs
반응형
반응형

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

 

5430번: AC

각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 값들을 입력받고, 전처리를 통해서 배열의 값들을 deque에 넣어줍니다.

 

2. R이 입력되면 rev변수를 갱신해 줍니다.

 

3. rev변수의 값에 따라 pop_back, 혹은 pop_front를 해줍니다.

 

4. deq에 남아있는 수를 rev변수에 따라 출력해 줍니다.

 

[ 소스코드 ]

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<deque>
#include<string>
 
using namespace std;
 
int T;
deque<int> deq;
 
void preproc(string x)
{
    int num = 0;
    for (auto next : x) {
        if (next >= '0' && next <= '9') {
            num *= 10;
            num += next - '0';
        }
        else if (next == ']' || next == ',') {
            deq.push_back(num);
            num = 0;
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    while (T--) {
        bool flag = true;
        bool rev = false;
        deq.clear();
        string p, x;
        int n;
        cin >> p >> n >> x;
 
        if (n) preproc(x);
 
        for (auto com : p) {
            if (com == 'R') {
                rev = !rev;
            }
            else {
                if (!deq.empty()) {
                    if (rev) {
                        deq.pop_back();
                    }
                    else {
                        deq.pop_front();
                    }
                }
                else {
                    cout << "error\n";
                    flag = false;
                    break;
                }
            }
            
        }
 
        if (flag) {
            cout << '[';
            while (!deq.empty()) {
                if (rev) {
                    if (deq.size() == 1) {
                        cout << deq.back();
                    }
                    else {
                        cout << deq.back() << ',';
                    }
                    deq.pop_back();
                }
                else {
                    if (deq.size() == 1) {
                        cout << deq.front();
                    }
                    else {
                        cout << deq.front() << ',';
                    }
                    deq.pop_front();
                }
            }
            cout << "]\n";
        }
    }
}
cs
반응형
반응형

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

 

3190번: 뱀

 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

www.acmicpc.net

 

 

[ 문제풀이 ]

 

덱(deque)이라는 자료구조를 활용하여 문제를 풀었습니다.

 

1. 사과의 위치와 시간에 따른 방향 정보를 입력받습니다.

 

2. 덱을 활용하여 머리와 꼬리의 정보를 저장합니다.

 

3. 사과를 먹으면 꼬리를 그대로 두고, 사과를 먹지 않는다면 꼬리를 덱에서 빼줍니다.

 

4. 벽이나 몸에 부딪히면 현재 시간을 출력해주고 종료합니다.

 

5. 정보를 바탕으로 배열에 값을 저장해줍니다.

 

[ 소스 코드 ]

 

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<deque>
 
using namespace std;
 
struct pos {
    int y;
    int x;
};
 
int N, K, L;
int arr[101][101];            //보드
int dy[4= { -1,0,1,0 };
int dx[4= { 0,1,0,-1 };
int dir = 1;
deque<pos> dq;
int cnt;
char direction[10001];        //시간 X이후 움직일 방향 저장할 배열
 
int main()
{
    scanf("%d %d"&N, &K);
    for (int i = 0; i < K; i++) {
        int a, b;
        scanf("%d %d"&a, &b);
        arr[a][b] = 1;
    }
    scanf("%d"&L);
    for (int i = 0; i < L; i++) {
        int a;
        char b;
        scanf("%d %c"&a, &b);
        direction[a] = b;
    }
 
    dq.push_back({ 1,1 });
    arr[1][1= 2;
 
    while (1)
    {
        int fy = dq.front().y;    //머리
        int fx = dq.front().x;
        int by = dq.back().y;    //꼬리
        int bx = dq.back().x;
 
        int yy = fy + dy[dir];    //이동할 좌표
        int xx = fx + dx[dir];
 
        cnt++;
 
        if (arr[yy][xx] == 2 || yy<1 || yy>|| xx<1 || xx>N) {    //몸통이거나 벽이라면
            printf("%d", cnt);
            return 0;
        }
 
        if (arr[yy][xx] != 1) {    //사과를 먹지 않으면
            arr[by][bx] = 0;
            dq.pop_back();
        }
 
        arr[yy][xx] = 2;
        dq.push_front({ yy,xx });
 
        if (direction[cnt] == 'L') {
            dir--;
        }
        else if (direction[cnt] == 'D') {
            dir++;
        }
 
        if (dir < 0) {
            dir = 3;
        }
        if (dir > 3) {
            dir = 0;
        }
    }
}
cs
반응형

+ Recent posts