Problem D Game of Throwns
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
5 4
8 -2 3 undo 2
Sample Output 1
3
Sample Input 2
5 10
7 -3 undo 1 4 3 -9 5 undo 2 undo 1 6
Sample Output 2
2
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int quee[105];
int top;
int n,m,order;
int val(char* cmd)
{
int flag=0,lenz=strlen(cmd),ans=0;
if(cmd[0]=='-') flag=1;
for(int i=flag;i<lenz;i++)
{
ans*=10;
ans+=cmd[i]-'0';
}
if(flag) ans*=-1;
return ans;
}
void undo(int order)
{
top-=order;
}
void thrw(int order)
{
int tmp=quee[top]+order;
while(tmp<0) tmp+=n;
quee[++top]=tmp%n;
}
int main()
{
char cmd[8];
while(~scanf("%d %d",&n,&m))
{
memset(quee,0,sizeof(quee));
top=0;
for(int i=0;i<m;i++)
{
scanf("%s",cmd);
if(!strcmp(cmd,"undo"))
{
scanf("%d",&order);
undo(order);
}
else
{
order=val(cmd);
thrw(order);
}
}
printf("%d\n",quee[top]);
}
return 0;
}
本博客介绍了一个名为“游戏掷龙蛋”的计算机科学教学游戏。游戏中孩子们围成一圈掷一颗石化龙蛋,根据老师的指令顺时针或逆时针传递龙蛋,并能撤销最近的指令。文章提供了游戏规则说明及一个实现此游戏逻辑的C++代码示例。
21万+

被折叠的 条评论
为什么被折叠?



