Topic: A robot, standing at the origin of a dimension axis, has a series of instructions, divided into: left one, right one, and step i the same;
Ask the last place.
Analysis: Simulation. Direct simulation can be.
The original position is 0, left-1, right +1, calculate the number and can.
stating: ╮(╯▽╰)╭.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;int Step[101];char temp[101];int main () {int T,n,same;while (~ scanf ("%d", &t)) while (T--) {scanf ("%d", &n), for (int i = 1; I <= n; + + i) {scanf ("%s", temp); if (!strcmp (temp, "left")) step[i] = -1;else if (!strcmp (temp, "right")) step[i] = 1;else {scanf ("%s%d", &temp,&same); Step[i] = step[ Same];}} int x = 0;for (int i = 1; I <= n; + + i) x + = Step[i];p rintf ("%d\n", x);} return 0;}
UVa 12503-robot Instructions