FZU 2091 播放器

时间:2023-11-13 16:17:02

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

#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=;
while(!S.empty()) S.pop();
} void Pri(int nowpos)
{
printf("%d\n",nowpos);
if(S.empty()||nowpos!=S.top()) S.push(nowpos);
} void PRE()
{
if(S.empty())
{
nowpos=;
Pri(nowpos);
}
else
{
S.pop();
if(S.empty())
{
nowpos=;
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[];
for(int i=;i<m;i++)
{
scanf("%s",s);
if(strcmp("PRE",s)==) PRE();
else if(strcmp("PLAY",s)==)
{
int y;
scanf("%d",&y);
PLAY(y);
}
else if(strcmp("NEXT",s)==) NEXT();
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
work();
}
return ;
}