Some children's Palace introduced a group of robot car. You can accept pre-entered instructions and act as directed. The basic movement of the trolley is simple, only 3 kinds: Turn left (remember as L), turn right (remember as R), walk forward several centimeters (directly remember the number).
For example, we can enter the following instructions for the car: 15L10R5LRR10R20, the car first straight line 15 cm, left, then walk 10 centimeters, then turn right, ...
It is not difficult to see, for this command string, the car back to the departure point.
Your task is: to write a program, by the user input instructions, the program output after each instruction to carry out the car position and the direction of the car before the position of the line distance.
Input and output format requirements
The user first enters an integer n (n<100), indicating that there will be n instructions next. Next, enter the n instruction. Each instruction consists of only L, R, and number (the number is an integer between 0~100), and each instruction is not more than 256 characters in length.
The program outputs n rows of results. Each result indicates a straight-line distance between the car and the position before and after the corresponding instruction. Requires rounding to 2 digits after decimal. For example: User input:
5
L100r50r10
3lll5rr4l12
LL
100R
5l5l5l5
The program output:
102.96
9.06
0.00
100.00
0.00
/** * Simulation robot walking, in four directions to indicate the direction of the robot walking * SSCANF (a+i, "%d", &cm); read a number from a string * when the reading number is 10 bits i++ 100 when the i++ to ensure that the string point*/#include<stdio.h>#include<math.h>intdx[4]={-1,0,1,0};intdy[4]={0,-1,0,1};DoubleFChara[]) {intx=0, y=0, dir=0, CM; for(intI=0; a[i]!=' /'; i++){if(a[i]=='L') dir= (dir+3)%4;Else if(a[i]=='R') dir= (dir+1)%4;Else{sscanf (a+i,"%d", &cm);//reading a number from a stringif(cm>=Ten) i++;//10-digit plus 1if(cm== -) i++;//100 digits in plus 1x=x+dx[dir]*Cm;y=y+dy[dir]*cm;}}returnsqrt (x*x+y*y);}intMain () {intN;Chara[ -]; while(SCANF ("%d",&N)) { for(intI=0; i<n;i++) {scanf ("%s", a);p rintf ("%.2LF", F (a));}}return 0;}
Blue Bridge Cup simulation robot walking