12503-robot instructions
You have a robot standing on the origin
XAxis. The robot will be given some instructions. Your taskis to predict its position after executing all the instructions.
- Left: Move one unit left (Decrease
PBy 1, wherePIs the position of the robot before moving)
- Right: Move one unit right (increase
PBy 1)
- SameI: Perform the same action as inI-Th instruction. it is guaranteed that
IIs a positiveinteger not greater than the number of instructions before this.
Input
The first line contains the number of test cases
T(T100). Each test case begins with an integerN(1N100 ),
The number of instructions. Each of the followingNLines contains an instruction.
Output
For each test case, print the final position of the robot. Note that after processing each test case, therobot shocould be reset to the origin.
Sample Input
23LEFTRIGHTSAME AS 25LEFTSAME AS 1SAME AS 2SAME AS 1SAME AS 4
Sample output
1-5
Solution: This article is entitled a simulated question. You only need to use an array to record the trend of each step. You can simply use it later. for the position, add the current movement directly.
# Include <stdio. h> # include <string. h> int main () {int t, n, x, sum; int A [105]; char C [10]; scanf ("% d", & T ); while (t --) {sum = 0; scanf ("% d", & N); For (INT I = 1; I <= N; I ++) {scanf ("% s", c); If (! Strcmp (C, "Left") // command processing, to the left a [I] =-1; else if (! Strcmp (C, "right") // command processing, to the right a [I] = 1; else // 3rd types of commands {scanf ("% s", C ); // absorb as scanf ("% d", & X); A [I] = A [X];} sum + = A [I];} printf ("% d \ n", sum);} return 0 ;}