gpt4 book ai didi

c++ - 推送和弹出操作的混合序列为什么这个序列不可能

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:08:51 25 4
gpt4 key购买 nike

我正在复习期末考试,但我无法解决这个问题:

Suppose that a client performs an intermixed sequence of stack push and pop operations. The push operations push the integers 0 through 9 in order on to the stack; the pop operations print out the return value. Which of the following sequences could not occur?

(a) 4 3 2 1 0 9 8 7 6 5
(b) 2 1 4 3 6 5 8 7 9 0
(c) 0 4 6 5 3 8 1 7 2 9
(d) 4 6 8 7 5 3 2 9 1 0
(e) All of these sequences are possible

答案是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。

关于c++ - 推送和弹出操作的混合序列为什么这个序列不可能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com