有关栈模拟的问题不会

题目描述

题目标题:
校赛-A:Valid Pop Sequence

题目描述:
In computer science, a stack is a last in, first out (LIFO) abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by two fundamental operations, called push and pop. The push operation adds a new item to the top of the stack.

Assume k integers is pushing into a stack in increasing order 1, 2, …, k. Please write a program that decides whether it is a valid pop sequence. For example, pushing 1, 2, 3 into a stack, (1, 2, 3) and (3, 2, 1) is a valid pop sequence, but (3, 1, 2) isn’t.

输入描述:
The input consists of several test cases.

For each test case, one integer k (1 <= k <= 100000) is given in the first line.

Second line contains k integers, presenting a pop sequence.

输出描述描述:
If the pop sequence is valid, print a line contains My Dearest. Otherwise it contains The Everlasting Guilty Crown

样式输入:
3
1 2 3
3
3 1 2

样式输出:
My Dearest
The Everlasting Guilty Crown

熟悉栈的特性就好做了,用数组模拟堆栈就可以了(因为是从1到k递增,不用数组也行)。正序输出是队列,逆序输出是栈,满足这两个条件就是符号条件的。
代码如下:

#include <iostream>
using namespace std;

int main()
{
    int i,k;
    int t,flag = 1;
    cin >> k;
    
    for(i=0;i<k;i++)
    {
        cin >> t;
        if(t == i+1 || t == k-i)
            continue;
        else
            flag = 0;
    }
    if(flag == 1)
        cout << "My Dearest";
    else
        cout << "The Everlasting Guilty Crown";
    return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632