1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include<cstdio> #include<cstring> #include<cmath> #define T 210
const double rate = 1 / sqrt(2); const double dir[8][2] = { { rate, rate }, { rate, -rate }, { -rate, -rate }, { -rate, rate }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; inline int getDir(char first, char second); int main() { char str[T]; int Case = 1; while (fgets(str, T, stdin)) { if (str[0] == 'E'&&str[1] == 'N') break;
int d, i; double pos_x = 0, pos_y = 0, dis;
for (i = 0;; i += 2) { dis = 0; while (str[i] <= '9'&&str[i] >= '0') { dis = dis * 10 + str[i] - '0'; i++; }
d = getDir(str[i], str[i + 1]);
pos_x += dir[d][0] * dis; pos_y += dir[d][1] * dis;
if (str[i + 1] != ','&&str[i + 1] != '.') i++;
if (str[i + 1] == '.') break; }
printf("Map #%d\n", Case++); printf("The treasure is located at (%.3lf,%.3lf).\n", pos_x, pos_y); printf("The distance to the treasure is %.3lf.\n\n", sqrt(pos_x*pos_x + pos_y*pos_y)); }
return 0; } int getDir(char first, char second) { if (second == 'E'&&first == 'N') return 0; else if (second == 'E'&&first == 'S') return 1; else if (second == 'W'&&first == 'S') return 2; else if (second == 'W'&&first == 'N') return 3; else if (first == 'N') return 4; else if (first == 'E') return 5; else if (first == 'S') return 6; else if (first == 'W') return 7; }
|