ECNA 2017 Problem D: Game of Throwns stack 模拟

Daenerys frequently invents games to help teach her second grade Computer Science class about various
aspects of the discipline. For this week’s lesson she has the children form a circle and (carefully) throw
around a petrified dragon egg.
The n children are numbered from 0 to n − 1 (it is a Computer Science class after all) clockwise around the
circle. Child 0 always starts with the egg. Daenerys will call out one of two things:
1. a number t, indicating that the egg is to be thrown to the child who is t positions clockwise from the
current egg holder, wrapping around if necessary. If t is negative, then the throw is to the counterclockwise
direction.
2. the phrase undo m, indicating that the last m throws should be undone. Note that undo commands
never undo other undo commands; they just undo commands described in item 1 above.
For example, if there are 5 children, and the teacher calls out the four throw commands 8 -2 3 undo 2,
the throws will start from child 0 to child 3, then from child 3 to child 1, then from child 1 to child 4. After
this, the undo 2 instructions will result in the egg being thrown back from child 4 to child 1 and then from
child 1 back to child 3. If Daenerys calls out 0 (or n, −n, 2n, −2n, etc.) then the child with the egg simply
throws it straight up in the air and (carefully) catches it again.
Daenerys would like a little program that determines where the egg should end up if her commands are
executed correctly. Don’t ask what happens to the children if this isn’t the case.
Input
Input consists of two lines. The first line contains two positive integers n k (1 ≤ n ≤ 30, 1 ≤ k ≤ 100)
indicating the number of students and how many throw commands Daenerys calls out, respectively. The
following line contains the k throw commands. Each command is either an integer p (−10 000 ≤ p ≤ 10 000)
indicating how many positions to throw the egg clockwise or undo m (m ≥ 1) indicating that the last m
throws should be undone. Daenerys never has the kids undo beyond the start of the game.
Output
Display the number of the child with the egg at the end of the game.
Sample Input 1 Sample Output 1
5 4
8 -2 3 undo 2
3

用stack 模拟一下即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 60005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

stack<int>sk;

int main()
{
    ios::sync_with_stdio(false);
    string s;
    int n, k;
    cin >> n >> k;
    while (k--) {
        cin >> s;
        if (s == "undo") {
            int tt;
            cin >> tt;
            while (tt--) {
                if (sk.empty())break;
                else sk.pop();
            }
        }
        else {
            stringstream ss;
            ss << s;
            int w;
            ss >> w;
            sk.push(w);
        }
    }
    int sum = 0;
    while (!sk.empty()) {
        int tp = sk.top();
        sk.pop();
        sum += tp;
    }
    cout << (sum%n + n) % n << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值