题目
输入:
5 4
8 -2 3 undo 2
输出
3
输入
5 10
7 -3 undo 1 4 3 -9 5 undo 2 undo 1 6
输出
2
题意:给出两个数n,m,n个孩子从1~n-1,m个操作,第一种操作输入一个x就是从当前孩子传给距离它x个单位的孩子(默认从第零个孩子开始传,孩子成环)
第二种操作undo x,表示返回前几个的状态,例如球从1-3-4-6,此时undo2,就是把球从6孩子给3孩子
思路:模拟,具体看代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
stack<int>h;
int main()
{
ios::sync_with_stdio(0);
while(!h.empty())h.pop();
char a[10];
int aa,n,m,sss=0;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int s,flag=0,oo=0;
cin>>a;
if(a[0]=='u')
{
cin>>aa;
while(aa--)
{
if(!h.empty())
h.pop();
}
if(!h.empty())sss=h.top();
else sss=0;
}
else
{
int o=1,k=strlen(a);
for(int j=k-1;j>=0;j--)
{
if(a[j]=='-')oo*=-1;
else
{
int y=a[j]-'0';
oo+=o*y;
o*=10;
}
}
sss=sss+oo;
while(sss<0)sss=sss+n;
sss%=n;
h.push(sss);//计算传给哪个孩子并入栈
}
}
int g;
if(!h.empty())g=h.top();
else g=0;
printf("%d",g);
}