题目链接:geohash编码
按照题意进行二分法就好了。
#include<iostream>
#include<algorithm>
using namespace std;
int cod[6],stc; //cod用来存geohash编码
int binary_search(int num);
int main()
{
int num;
while(cin>>num)
{
binary_search(num);
for(int i=0;i<stc;++i)
{
cout<<cod[i];
}
cout<<endl;
}
return 0;
}
int binary_search(int num) //按照题意进行二分
{
stc=0;
int cnt=6;
int low=-90,high=90;
int mid=(low+high)/2;
while(cnt--)
{
// cout<<low<<" "<<high<<endl; 检验边界
mid=(low+high)/2;
if(num>=mid)
{
low=mid;
cod[stc++]=1;
}
else if(num<mid)
{
high=mid;
cod[stc++]=0;
}
}
return 0;
}