⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 地球距离.cpp

📁 可以计算地球上任意两点间的距离 代码简洁清晰可靠
💻 CPP
字号:
/* Contest   : Ulm Local Contest 1997 
 * Problem G : Globetrotter
 * PKU 2254
 * Method    : Vector Geometry
 * Author    : Mark Dettinger
 * Date      : June 13, 1997
 */

#include <stdio.h>
#include <string.h>
#include <math.h>

#define PI 3.141592653589793
#define R 6378

typedef struct { char name[30]; double x,y,z; } city;

FILE* input;

int cities=0;
city c[1000];

int read_city()
{
  double longitude,latitude;

  /* read city description */

  fscanf(input,"%s",c[cities].name);
  if (strcmp(c[cities].name,"#")==0) return 0;
  fscanf(input,"%lf %lf",&latitude,&longitude);

  /* compute 3-D coordinate representation of city */

  longitude *= PI/180.0;
  latitude *= PI/180.0;
  c[cities].x = sin(longitude)*cos(latitude);
  c[cities].y = cos(longitude)*cos(latitude);
  c[cities].z = sin(latitude);

  /* insert city into table */

  cities++;
  return 1;
}

double distance (int i, int j)  /* distance between city i and city j */
{
  double alpha = acos(c[i].x*c[j].x+c[i].y*c[j].y+c[i].z*c[j].z);
  return alpha*R;
}
 
int solve_case()
{
  char src[30],dst[30];
  int i,j;

  /* read source and destination city */

  fscanf(input,"%s %s",src,dst);
  if (strcmp(src,"#")==0) return 0;
  printf("%s - %s\n",src,dst);

  /* search table for cities */

  for (i=0; i<cities && strcmp(c[i].name,src); i++);
  for (j=0; j<cities && strcmp(c[j].name,dst); j++);

  /* compute and print distance between cities */

  if (i==cities || j==cities)
    printf("Unknown\n\n");
  else
    printf("%.0f km\n\n",distance(i,j));
  return 1;
}

int main()
{
  input = fopen("globetrotter.in","r");
  while (read_city());
  while (solve_case());
  fclose(input);
  return 0;
}


/*
problem pku 3407
*/

#include <iostream>
#include <math.h>

#define pi acos((double)-1)
#define r 6370
#define sq(x) ((x)*(x))
#define ss (2*(pi)*(r))

using namespace std;

double wd[3],wf[3],jd[3],jf[3] , w , j;
double a,b,x[3],y[3],z[3];
char ns[3],we[3];

int main ()
{
	int i;
	for( i = 1 ; i < 3 ; i++ )
	{
		cin>>wd[i]>>wf[i]>>ns[i]>>jd[i]>>jf[i]>>we[i];
		w = wd[i] + wf[i] / 60;
		if (ns[i] == 'S' && w < 90) 
			w = -w;
		w = w * ( pi / 180 );
		j = jd[i] + jf[i] / 60;
		if (we[i] == 'W' && j < 180) 
			j = -j;
		j = j * ( pi / 180 );
		z[i]=r*sin(w);
		x[i]=r*cos(j)*cos(w);
		y[i]=r*sin(j)*cos(w);
	}
	b=(sq(r)*2-(sq(z[1]-z[2])+sq(x[1]-x[2])+sq(y[1]-y[2])))/(2*sq(r));
	a=acos(b);
	b=a*r;
	if(b>ss/2)b=ss-b;
	printf ("%0.3lf\n",b);
	return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -