题目:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

3N,1E,1N,3E,2S,1W. 10NW. END
Map #1 The treasure is located at (3.000,2.000). The distance to the treasure is 3.606. Map #2 The treasure is located at (-7.071,7.071). The distance to the treasure is 10.000.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
题目不需要设计算法,关键在于怎么控制输入以及如何计算方向。
可以先将一行数据输入到一个字符数组或者string对象里面,然后逐个字符解释。
计算方向的时候有“N”与“NW”的区分,要考虑两个字符。用if或者switch的话罗列的情况比较多,这种问题一般先开个相关的数组,再利用索引统一计算。
 1 #include <iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7     string str;
 8     int st[90][2]={0},c=1;                  //st数组索引x与y增长的方向
 9     double g=sqrt(2.0)/2;
10     st[‘N‘][1]=st[‘E‘][0]=1;
11     st[‘S‘][1]=st[‘W‘][0]=-1;
12     while(cin>>str)
13     {
14         if(str=="END")
15             break;
16         int num=0,len=str.length()-1;
17         double x=0,y=0,r=0;
18         for(int i=0;i<len;i++)
19         {
20             if(str[i]>=‘0‘&&str[i]<=‘9‘)
21                 num=num*10+str[i]-‘0‘;
22             if(str[i]>‘A‘&&str[i]<‘Z‘)
23             {
24                 if(str[i+1]>‘A‘&&str[i+1]<‘Z‘)
25                 {
26                     x+=num*g*(st[str[i]][0]+st[str[i+1]][0]);
27                     y+=num*g*(st[str[i]][1]+st[str[i+1]][1]);
28                 }
29                 else
30                 {
31                     x+=num*st[str[i]][0];
32                     y+=num*st[str[i]][1];
33                 }
34                 num=0;                                                 //num在这置零
35             }
36         }
37         r=sqrt(x*x+y*y);
38         cout<<"Map #"<<c<<endl;
39         cout<<"The treasure is located at ("<<fixed<<setprecision(3)<<x<<","<<y<<")."<<endl;
40         cout<<"The distance to the treasure is "<<r<<"."<<endl<<endl;
41         c++;
42     }
43     return 0;
44 }
There's Treasure Everywhere! poj1473题目解析
原文:http://www.cnblogs.com/asingingbird/p/4598791.html