-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq2.cpp
More file actions
47 lines (39 loc) · 779 Bytes
/
q2.cpp
File metadata and controls
47 lines (39 loc) · 779 Bytes
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
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
// 2. 出栈序列统计
// 并不需要真的构造一个栈
void recursive(int &source, int &stack, int &count) {
if (source == 0 && stack == 0) {
// 一种方式结束
count++;
return;
} else if (stack == 0) {
stack++;
source--;
recursive(source, stack, count);
} else if (source == 0) {
stack--;
recursive(source, stack, count);
} else {
int stack2 = stack;
int source2 = source;
// 进栈
stack2++;
source2--;
recursive(source2, stack2, count);
// 出栈
stack--;
recursive(source, stack, count);
}
}
int main(int argc, char *argv[]) {
int n = 0;
cin >> n;
int count = 0;
int stack = 0;
recursive(n, stack, count);
cout << count;
return 0;
}