FZU 2091 播放器

简单模拟题,开个栈维护一下即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<iostream>
using namespace std;

stack<int>S;
int n,m;
int nowpos;//播放列表播放到哪一首

void init()
{
    nowpos=1;
    while(!S.empty()) S.pop();
}

void Pri(int nowpos)
{
    printf("%d
",nowpos);
    if(S.empty()||nowpos!=S.top()) S.push(nowpos);
}

void PRE()
{
    if(S.empty())
    {
        nowpos=1;
        Pri(nowpos);
    }
    else
    {
        S.pop();
        if(S.empty())
        {
            nowpos=1;
            Pri(nowpos);
        }
        else
        {
            nowpos=S.top();
            Pri(nowpos);
        }
    }
}

void PLAY(int num)
{
    nowpos=num;
    Pri(nowpos);
}

void NEXT()
{
    if(nowpos==n)
    {
        Pri(nowpos);
    }
    else
    {
        nowpos++;
        Pri(nowpos);
    }
}

void work()
{
    char s[10];
    for(int i=0;i<m;i++)
    {
        scanf("%s",s);
        if(strcmp("PRE",s)==0) PRE();
        else if(strcmp("PLAY",s)==0)
        {
            int y;
            scanf("%d",&y);
            PLAY(y);
        }
        else if(strcmp("NEXT",s)==0) NEXT();
    }
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        work();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5242604.html