Gym - 101673D Game of Throwns【模拟】

题目

传送门
在这里插入图片描述

输入:
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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值