zoj 1943 || poj 2254 || UVA 535 Globetrotter ( UVA卡精度了 )

211 篇文章 0 订阅
134 篇文章 1 订阅

蛮水的题,给你N个城市的大地坐标,然后询问。

我查询用map了,应该会快一点哈~好方便滴说。

其他没啥了,就求下距离。

zoj poj AC的代码是先求角度,然后 ang*r是答案。

结果捏 UVA 死活过不去,后来看UVA的discuss,看了人家求距离的函数,贴了过来,过了 = = 。。

应该我的先求角度再求距离的方法被卡精度了。

贴上UVA AC的代码。


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const int MAX = 101;
const double r = 6378;
const double pi = 3.141592653589793;
struct NODE{ double lng,lat;};
NODE p[MAX];

map<string, int> m;

double dis_3d(double lng1, double lat1, double lng2, double lat2)  
{           //经度,纬度,经度,纬度   
	double dlon = lng2 - lng1; 
	double dlat = lat2 - lat1; 
 	double a = pow((sin(dlat/2)),2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2), 2); 
 	double c = 2 * atan2(sqrt(a), sqrt(1-a)); 
 	double d = r * c; 
 	return d;
}
int main()
{
	int i = 0;
	
	string s, s1, s2;

	while( cin >> s )
	{
		if( s == "#" ) break;
		cin >> p[i].lat >> p[i].lng;
		m.insert( make_pair(s, i) );
		i++;
	}

	while( cin >> s1 >> s2 )
	{
		if( s1 == "#" && s2 == "#" ) break;
		
		if( !m.count(s1) || !m.count(s2) )
		{
			cout << s1 << " - " << s2 << endl;
			cout << "Unknown" << endl;
			continue;
		}
		int a = m.find(s1)->second;
		int b = m.find(s2)->second;
		
		double dis = dis_3d(p[a].lng*pi/180, p[a].lat*pi/180, 
							  p[b].lng*pi/180, p[b].lat*pi/180);
		
		dis = floor(dis + 0.5);
		cout << s1 << " - " << s2 << endl;
		cout << dis << " km" << endl;
	}

return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值