There's Treasure Everywhere!
Blog original address: http://blog.csdn.net/xuechelingxiao/article/details/40865611
Question:
Here is a string with many operations. The preceding number is the moving distance, and the following English indicates the moving direction, ask where the last point of departure from the distant point falls and the distance from the starting point.
Solution:
The question itself is not very difficult, and there is no pitfall, nothing to say, just be careful when dealing with strings.
PS: a carriage return is required for each group, because this PE is used once!
Code:
# Include <stdio. h> # include <iostream> # include <algorithm> # include <string> # include <math. h> using namespace std; const double t = sqrt (2.0); int main () {string s; int icase = 1; while (cin> s) {if (s = "END") {break;} int num = 0; double x = 0, y = 0; for (int I = 0; I <(int) s. length ()-1; ++ I) {if (s [I]> = '0' & s [I] <= '9 ') {num = num * 10 + (s [I]-'0');} else if (s [I] = ',') {num = 0; continue ;} else if (s [I] = 'n' & s [I + 1] = 'e') {x + = num/t, y + = num/t, I ++;} else if (s [I] = 'n' & s [I + 1] = 'w ') {x-= num/t, y + = num/t, I ++ ;} else if (s [I] = 'S' & S [I + 1] = 'e') {x + = num/t, y-= num/t, I ++;} else if (s [I] = 'S' & S [I + 1] = 'w ') {x-= num/t, y-= num/t, I ++;} else if (s [I] = 'n') {y + = num ;} else if (s [I] = 's') {y-= num;} else if (S [I] = 'e') {x + = num ;} else if (s [I] = 'w') {x-= num;} // printf ("% lf \ n", x, y );} printf ("Map # % d \ n", icase ++); printf ("The treasure is located at (%. 3lf, %. 3lf ). \ n ", x, y); printf (" The distance to the treasure is %. 3lf. \ n ", sqrt (x * x + y * y);} return 0 ;}
POJ 1473 There's Treasure Everywhere! (Simple ry)