http://acm.timus.ru/problem.aspx?space=1&num=1030
以地球球心为坐标原点,建立三维坐标系,求出两点坐标,计算直线距离,求出夹角,再乘以半径。
1 #include <iostream>
2 #include <math.h>
3 using namespace std;
4 const long double D=6875;
5 const long double pi=3.1415926535897932384626433;
6 struct postype{
7 long double x,y,z;
8 };
9 int x1,x2,x3,yy1,y2,y3;
10 int sx1,sx2,sx3,syy1,sy2,sy3;
11 char str[100000];
12 char xstr[10],ystr[10],sxstr[10],systr[10];
13 postype ship,ice;
14 long double angle1,angle2;
15
16 long double dis(double a,double b,double c,double d,double e,double f){
17 a-=d;
18 b-=e;
19 c-=f;
20 return sqrt(a*a+b*b+c*c);
21 }
22
23 int main(){
24 for (int i=1;i<=3;++i) gets(str);
25 scanf("%d^%d'%d\" %s\n",&x1,&x2,&x3,&xstr);
26 scanf("and %d^%d'%d\" %s\n",&yy1,&y2,&y3,&ystr);
27 gets(str);
28 scanf("%d^%d'%d\" %s\n",&sx1,&sx2,&sx3,&sxstr);
29 scanf("and %d^%d'%d\" %s\n",&syy1,&sy2,&sy3,&systr);
30 gets(str);
31
32 double a=x1,b=x2,c=x3;
33 angle1=a*3600+b*60.0+c;
34 angle1/=3600.0*180;
35 angle1*=pi;
36 if (xstr[0]=='S') angle1=-angle1;
37 a=yy1;b=y2;c=y3;
38 angle2=a*3600+b*60.0+c;
39 angle2/=3600.0*180;
40 angle2*=pi;
41 if (ystr[0]=='W') angle2=-angle2;
42 ship.x=D/2*cos(angle1)*cos(angle2);
43 ship.y=D/2*cos(angle1)*sin(angle2);
44 ship.z=D/2*sin(angle1);
45
46 a=sx1,b=sx2,c=sx3;
47 angle1=a*3600+b*60+c;
48 angle1/=3600.0*180;
49 angle1*=pi;
50 if (sxstr[0]=='S') angle1=-angle1;
51 a=syy1;b=sy2;c=sy3;
52 angle2=a*3600+b*60+c;
53 angle2/=3600.0*180;
54 angle2*=pi;
55 if (systr[0]=='W') angle2=-angle2;
56 ice.x=D/2*cos(angle1)*cos(angle2);
57 ice.y=D/2*cos(angle1)*sin(angle2);
58 ice.z=D/2*sin(angle1);
59
60 long double linedis=dis(ship.x,ship.y,ship.z,ice.x,ice.y,ice.z);
61 linedis/=2;
62 long double AA=asin(linedis*2/D)*2;
63 long double ans=AA;
64 ans=ans*D/2;
65
66 printf("The distance to the iceberg: ");
67 cout.precision(2);
68 cout<<fixed;
69 cout<<ans;
70 printf(" miles.\n");
71 if (100.00-ans>0.005) printf("DANGER!");
72 }
73
posted on 2008-11-04 16:05
Joseph 阅读(241)
评论(0) 编辑 收藏 引用