/*城市间的球面距离(求两地之间的最短球面距离)
样例输入
3
Beijing 116.47 39.9
Hangzhou 120.15 30.23
Shanghai 121.48 31.23
样例输出
Start-End Beijing Hangzhou Shanghai
Beijing 0.00 1125.91 1064.77
Hangzhou 1125.91 0.00 168.89
Shanghai 1064.77 168.89 0.00
*/
思路分析:由题意可知,所求问题是平面与地球相交形成的大圆的劣弧长度 ,通过画图计算可得如下公式:A,B两点间的最短球面距离=地球平均半径*arccos(cosA*cosB*cos(A,B两地经度差的绝对值)+sinA*sinB)
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cmath>
#define pi 3.1415926
using namespace std;
typedef struct
{
char cname[15];
double lat;
double lon;
}
city;
typedef struct
{
int cities;
city citices[40];
}
citieslist;
citieslist readfile()//读取数据
{
citieslist cl;
cin>>cl.cities;
for(int i=0;i<cl.cities;i++)
{
cin>>cl.citices[i].cname>>cl.citices[i].lon>>cl.citices[i].lat;
cl.citices[i].lat = cl.citices[i].lat*pi/180.0;
cl.citices[i].lon = cl.citices[i].lon*pi/180.0;
}
return cl;
}
void caldis(citieslist cl,double d[40][40])//计算球面距离
{
for(int i=0;i<cl.cities;i++)
for(int j=0;j<cl.cities;j++)
{
d[i][j] = 6371.0*acos(cos(cl.citices[i].lat)*cos(cl.citices[j].lat)*cos(cl.citices[j].lon-cl.citices[i].lon)+sin(cl.citices[i].lat)*sin(cl.citices[j].lat));
}
}
void writedislist(citieslist cl,double d[40][40])//输出距离表
{
cout<<setw(10)<<setiosflags(ios::left)<<"Start-End";
for(int i=0;i<cl.cities;i++)
cout<<setw(10)<<setiosflags(ios::left)<<cl.citices[i].cname;
cout<<endl;
for(int i=0;i<cl.cities;i++)
{
cout<<setw(10)<<cl.citices[i].cname;
for(int j=0;j<cl.cities;j++)
cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<d[i][j];
cout<<endl;
}
}
int main()
{
citieslist cl;
double d[40][40];
cl = readfile();
caldis(cl,d);
writedislist(cl,d);
return 0;
}
连载二
网友评论