首页 > 代码库 > poj2354Titanic(两点的球面距离)

poj2354Titanic(两点的球面距离)

链接

球面距离计算公式:d(x1,y1,x2,y2)=r*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2))
x1,y1是纬度\经度的弧度单位,r为地球半径
 
把有用的信息从恶心的字符串中提取出来,然后套公式,再处理下恶心的精度,得出的距离需要四舍五入。
 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set>10 using namespace std;11 #define N 10000012 #define LL long long13 #define INF 0xfffffff14 const double eps = 1e-8;15 const double pi = acos(-1.0);16 const double inf = ~0u>>2;17 const double r = 6875/2.0;18 char s[110];19 int dcmp(double x)20 {21     if(fabs(x)<eps) return 0;22     return x<0?-1:1;23 }24 void solve(double x1,double y1,double x2,double y2)25 {26     double ans =  r*acos(sin(x1)*sin(x2)+27                    cos(x1)*cos(x2)*cos(y1-y2));28 29     printf("The distance to the iceberg: %.2lf miles.\n",ans);30     if(dcmp(ans-100+0.005)<0)31     puts("DANGER!");32 }33 int main()34 {35     double d1,d2,d3,d4;36     int a,b,c,i;37     char dir[10];38     for(i = 0; i < 9 ; i++)39     scanf("%s",s);40     scanf("%s",s);41     sscanf(s,"%d^%d‘%d‘‘",&a,&b,&c);42     //printf("%d %d %d\n",a,b,c);43     d1 = a+b/60.0+c/3600.0;44     scanf("%s",dir);45     if(dir[0]==S)46     d1=-d1;47     scanf("%s",s);48 49     scanf("%s",s);50     sscanf(s,"%d^%d‘%d‘‘",&a,&b,&c);51     //printf("%d %d %d\n",a,b,c);52     d2 = a+b/60.0+c/3600.0;53     scanf("%s",dir);54     if(dir[0]==W)55     d2=-d2;56 57     for(i =0 ; i < 5 ; i++) scanf("%s",s);58     scanf("%s",s);59     sscanf(s,"%d^%d‘%d‘‘",&a,&b,&c);60     //printf("%d %d %d\n",a,b,c);61     d3 = a+b/60.0+c/3600.0;62     scanf("%s",dir);63     if(dir[0]==S)64     d3=-d3;65 66     scanf("%s",s);67 68     scanf("%s",s);69     sscanf(s,"%d^%d‘%d‘‘",&a,&b,&c);70     //printf("%d %d %d\n",a,b,c);71     d4 = a+b/60.0+c/3600.0;72     scanf("%s",dir);73     if(dir[0]==W)74     d4=-d4;75     scanf("%s",s);76 77     solve(d1*pi/180,d2*pi/180,d3*pi/180,d4*pi/180);78     return 0;79 }
View Code