我正在为决赛做研究,但无法弄清楚这个问题:

答案是C,但我不确定如何得出该结论

最佳答案

好的,所以程序总是按该顺序推0-9,因此,查看每一行,我们可以得出推和弹出发生的顺序

**The first line.**   - Stack is
Pushes 0, 1, 2, 3, 4  - [0, 1, 2, 3, 4]
Pops   4, 3, 2, 1, 0  - []
Pushes 5, 6, 7, 8, 9  - [5, 6, 7, 8, 9]
Pops   9, 8, 7, 6, 5  - []

**Second line**  - Stack is
Pushes 0, 1, 2   - [0, 1, 2]
Pops   2, 1      - [0]
Pushes 3, 4      - [0, 3, 4]
Pops   4, 3      - [0]
Pushes 5, 6      - [0, 5, 6]
Pops   6, 5      - [0]
Pushes 7, 8      - [0, 7, 8]
Pops   8, 7      - [0]
Pushes 9         - [0, 9]
Pops   9, 0      - []

**Third line**    - Stack is
Pushes 0          - [0]
Pops   0          - []
Pushes 1, 2, 3, 4 - [1, 2, 3, 4]
Pops   4,         - [1, 2, 3]
Pushes 5, 6       - [1, 2, 3, 5, 6]
Pops   6, 5, 3    - [1, 2]
Pushes 7, 8       - [1, 2, 7, 8]
Pops   8          - [1, 2, 7]
Pops   ?

下一个pop 必须必须为7,因为它在8之前被推送,因此不能为1。

10-08 04:14