B - 机器人的指令

xiaoxiao2021-02-27  175

数轴原点有一个机器人。该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置。

·LEFT:往左移动一个单位

·RIGHT: 往右移动一个单位

·SAME AS i: 和第i 条执行相同的动作。输入保证i 是一个正整数,且不超过之前执行指令数

Input

输入第一行为数据组数T (T<=100)。每组数据第一行为整数n (1<=n<=100),即指令条数。以下每行一条指令。指令按照输入顺序编号为1~n。

Output

对于每组数据,输出机器人的最终位置。每处理完一组数据,机器人应复位到数轴原点。

Sample Input 2 3 LEFT RIGHT SAME AS 2 5 LEFT SAME AS 1 SAME AS 2 SAME AS 1 SAME AS 4 Sample Output 1 -5

#include<cstdio>

#include<cstring> #include<iostream> using namespace std; int main() {     int repeat;     int n;     int pp[100+10];     memset(pp,0,sizeof(pp));     int count2 =0;     scanf("%d",&repeat);     while(repeat--)     {         int count1=0;         scanf("%d",&n);         while(n--)         {             char xx[10],ss[10];             scanf("%s",xx);             scanf("%s",ss);             if(xx[0] == 'L')             {                 count1--;                 pp[count2++]=-1;             }             else                 if(xx[0] == 'R'){                     count1++;                     pp[count2++]=1;                 }             else             {                 int c;                 scanf("%d",&c);                 if(pp[c-1]==1)                     count1++;                 else if(pp[c-1]==-1)                     count1--;             }         }         printf("%d\n",count1);     } }
转载请注明原文地址: https://www.6miu.com/read-15685.html

最新回复(0)