首页 > 试题广场 >

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; 已达到精度要求,编码为111100。 
样本输入:80 样本输出:111100
#include<iostream> 
using namespace std;  
  
string Solve(int n)  
{  
    string num;  
    int start = -90;  
    int end = 90;  
    int mid = 0;  
    for(int i = 0;i<6&&start<end;i++)  
    {  
        mid = start+(end-start)/2;  
        if(n<mid)  
        {  
            end = mid;  
            num+='0';  
        }  
        else  
        {  
            start = mid;  
            num+='1';  
        }  
    }  
    return num;  
}  
int main()  
{  
    int n;  
    while(cin>>n){
        string ret = Solve(n);  
    cout<<ret<<endl; 
    }  
    return 0;
} 
90%,谁给看看哪的问题?
发表于 2017-09-03 22:36:32 回复(1)

# -*- coding:utf-8 -*-
import math
def geohash_coding(n): # n [-90,90]
    precise = 6 #精度
    start = -90
    end = 90
    code = []
    i = 0
    while i < precise:
        med = math.floor((start+end)/2)
        if (n > med and n<0) or (n >= med and n>=0) :
            code.append('1')
            start = med
        else:
            code.append('0')
            end = med
        i += 1
    return ''.join(code)

if __name__ == '__main__':
    num = input() 
    print geohash_coding(n = num)
        
发表于 2017-08-04 19:04:04 回复(0)
import math
code = [] # 存储输出编码
count = 6 # 编码精度
start = -90 # 区间开始
end = 90  # 区间结束
num = 80 # 要进行编码的维度
for i in range(count):
    bet = math.floor((start + end)/2) # 二分向下取整
    if(num >= bet): # 中间值取右
        start = bet
        code.append(1)
    else:
        end = bet
        code.append(0)

发表于 2017-06-04 14:29:19 回复(1)
# _*_ coding:utf-8 _*_
right_list = [] # 左区间
left_list = [] # 右区间
geohash_code = []
precision = 6 # 编码精度
# 生成区间为[-90,90]的列表
def whole_list(right,left):
    wholelist = []
    for i in range(right,left+1):
        wholelist.append(i)
    return wholelist
whole_list = whole_list(-90,90)
# geohash编码
def geohash(check_number):
    the_whole_list = whole_list
    left_list = the_whole_list[:the_whole_list.index(0)]
    right_list = the_whole_list[the_whole_list.index(0):]
    pre  = precision
    check_number = int(check_number)
    for p in range(1,pre+1):
        if check_number in right_list:
            geohash_code.append(1)
            the_whole_list = right_list
            key = (the_whole_list[0]+the_whole_list[-1])//2
            left_list = the_whole_list[:the_whole_list.index(key)+1]
            right_list = the_whole_list[the_whole_list.index(key):]
        elif check_number in left_list:
            geohash_code.append(0)
            the_whole_list = left_list
            key = (the_whole_list[0]+the_whole_list[-1])//2
            left_list = the_whole_list[:the_whole_list.index(key)+1]
            right_list = the_whole_list[the_whole_list.index(key):]
        else:
            check_number = raw_input(u'您所查询的经纬度不在此区间,请在-90~90之间选择并重新输入:' )
            geohash_code(check_number)
def main():
    check_name = raw_input(u'请输入您想要查询的经纬度:')
    geohash(check_name)
    print geohash_code
if __name__ == '__main__':
    main()

发表于 2017-05-23 15:17:02 回复(0)

发表于 2017-01-20 17:03:05 回复(0)