球面距离计算公式:d(x1,y1,x2,y2)=r*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2))
x1,y1是纬度\经度的弧度单位,r为地球半径
把有用的信息从恶心的字符串中提取出来,然后套公式,再处理下恶心的精度,得出的距离需要四舍五入。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
const double r = /2.0;
char s[];
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
void solve(double x1,double y1,double x2,double y2)
{
double ans = r*acos(sin(x1)*sin(x2)+
cos(x1)*cos(x2)*cos(y1-y2)); printf("The distance to the iceberg: %.2lf miles.\n",ans);
if(dcmp(ans-+0.005)<)
puts("DANGER!");
}
int main()
{
double d1,d2,d3,d4;
int a,b,c,i;
char dir[];
for(i = ; i < ; i++)
scanf("%s",s);
scanf("%s",s);
sscanf(s,"%d^%d'%d''",&a,&b,&c);
//printf("%d %d %d\n",a,b,c);
d1 = a+b/60.0+c/3600.0;
scanf("%s",dir);
if(dir[]=='S')
d1=-d1;
scanf("%s",s); scanf("%s",s);
sscanf(s,"%d^%d'%d''",&a,&b,&c);
//printf("%d %d %d\n",a,b,c);
d2 = a+b/60.0+c/3600.0;
scanf("%s",dir);
if(dir[]=='W')
d2=-d2; for(i = ; i < ; i++) scanf("%s",s);
scanf("%s",s);
sscanf(s,"%d^%d'%d''",&a,&b,&c);
//printf("%d %d %d\n",a,b,c);
d3 = a+b/60.0+c/3600.0;
scanf("%s",dir);
if(dir[]=='S')
d3=-d3; scanf("%s",s); scanf("%s",s);
sscanf(s,"%d^%d'%d''",&a,&b,&c);
//printf("%d %d %d\n",a,b,c);
d4 = a+b/60.0+c/3600.0;
scanf("%s",dir);
if(dir[]=='W')
d4=-d4;
scanf("%s",s); solve(d1*pi/,d2*pi/,d3*pi/,d4*pi/);
return ;
}