You are currently at the origin (0, 0) and will be given commands to either go Right (R), Left (L), Up (U) or Down (D) by a certain number of steps. At the end of all these commands, you will be signaled to stop by reading the character ‘E’, after which you need to output your position in the x-y plane. The four kinds of movements are the following (direction followed by number of steps in that direction):
R number_of_steps : You need to increase your x-coordinate by “number_of_steps”.
L number_of_steps : You need to decrease your x-coordinate by “number_of_steps”.
U number_of_steps : You need to increase your y-coordinate by “number_of_steps”.
D number_of_steps : You need to decrease your y-coordinate by “number_of_steps”.
INPUT:
Direction number_of_steps (a character and integer separated by a space)
.
.
.
E (command to stop)
OUTPUT:
x y
SOLUTION:
int main() { char U, D, R, L, E; int x = 0, y = 0; int steps; char direction; while (1) { cin >> direction; cin >> steps; switch (direction) { case 'U': y += steps; break; case 'D': y -= steps; break; case 'R': x += steps; break; case 'L': x -= steps; break; case 'E': cout << x << " " << y<<endl; default: exit(0); } } return 0; }
Comments
Post a Comment