Ultraviolet A 12503-robot instructions

Source: Internet
Author: User
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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.