Poj2354titanic (spherical distance between two points)

Source: Internet
Author: User
Tags acos

Link

Formula for Calculating the spherical distance: D (x1, Y1, X2, Y2) = r * arccos (sin (X1) * sin (X2) + cos (X1) * Cos (X2) * Cos (y1-y2) x1, Y1 is the unit of radians of latitude \ longitude, r extracts useful information from the disgusting string for the Earth's radius, and then sets the formula, after processing the disgusting precision, the distance obtained must be rounded down.
 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.