标签:
题目:你开始在坐标原点,想去目的地,给你一系列的指路信息,问目的地的位置和到原点的距离。
分析:模拟,计算几何。直接按照顺序计算即可,利用相对坐标求绝对坐标。
说明:注意输入格式。
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int temp; char buf[10]; double x,y; int input(void) { scanf("%d",&temp); int count = 0; while (buf[count] = getchar()) { if (buf[count] == '.' || buf[count] == ',') break; count ++; } int flag = 1; if (buf[count] == '.') flag = 0; buf[count] = 0; return flag; } void deal(void) { if (buf[0] == 'W') { x -= temp;return; } if (buf[0] == 'E') { x += temp;return; } if (buf[0] == 'N') { if (buf[1] == 'W') { x -= temp*sqrt(0.5);y += temp*sqrt(0.5);return; } if (buf[1] == 'E') { x += temp*sqrt(0.5);y += temp*sqrt(0.5);return; } y += temp;return; } if (buf[0] == 'S') { if (buf[1] == 'W') { x -= temp*sqrt(0.5);y -= temp*sqrt(0.5);return; } if (buf[1] == 'E') { x += temp*sqrt(0.5);y -= temp*sqrt(0.5);return; } y -= temp;return; } } int main() { int T = 1,B; while ((B = getchar()) != 'E') { ungetc(B, stdin); x = y = 0.0; while (input()) deal(); deal();getchar(); printf("Map #%d\n",T ++); printf("The treasure is located at (%.3lf,%.3lf).\n",x,y); printf("The distance to the treasure is %.3lf.\n\n",sqrt(x*x+y*y)); } return 0; }
UVa 587 - There's treasure everywhere!
标签:
原文地址:http://blog.csdn.net/mobius_strip/article/details/42685359