geohash,暴力递归

geohash编码:geohash常用于将二维的经纬度转换为字符串,分为两步:第一步是经纬度的二进制编码,第二步是base32转码。
此题考察纬度的二进制编码:算法对纬度[-90, 90]通过二分法进行无限逼近(取决于所需精度,本题精度为6)。注意,本题进行二分法逼近过程中只采用向下取整来进行二分,针对二分中间值属于右区间。算法举例如下: 针对纬度为80进行二进制编码过程:
1) 区间[-90, 90]进行二分为[-90, 0),[0, 90],成为左右区间,可以确定80为右区间,标记为1;
2) 针对上一步的右区间[0, 90]进行二分为[0, 45),[45, 90],可以确定80是右区间,标记为1;
3) 针对[45, 90]进行二分为[45, 67),[67,90],可以确定80为右区间,标记为1;
4) 针对[67,90]进行二分为[67, 78),[78,90],可以确定80为右区间,标记为1;
5) 针对[78, 90]进行二分为[78, 84),[84, 90],可以确定80为左区间,标记为0;
6) 针对[78, 84)进行二分为[78, 81), [81, 84),可以确定80为左区间,标记为0;

输入描述:

输入包括一个整数n,(-90 ≤ n ≤ 90)

输出描述:

输出二进制编码

输入

 

80

输出

 

111100

 

#include <vector>
#include <iostream>
using namespace std;
int partition(const int &min, const int &max)
{
	return (min + max) / 2;

}
void geohash(vector<int>& output, const int &x, const int &min, const int &max)
{
	if ((min + 3) != max)
	{
		if (x >= partition(min, max))
		{	output.push_back(1);
			geohash(output, x, partition(min, max), max);
			
		}
		else
		{	output.push_back(0);
			geohash(output, x, min, partition(min, max));
			
		}
	}
}
int main()
{
	vector<int> output;
	int x;
	cin >> x;
	geohash(output, x, -90, 90);
	for (auto i = output.begin(); i != output.end(); i++)
		cout << *i;



	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值