C#练习题答案: Geohashing【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

Geohashing【难度:3级】:

答案1:

using System;
using System.Security.Cryptography;
using System.Text;

public class Kata
{
   
  public static Tuple<double, double> GeoHashing(double dow, DateTime? date = null)
  {
   
    DateTime dt = date ?? DateTime.UtcNow.Date;
    string newString = MakeString(dow, dt);
    newString = CreateMD5(newString);
    string firstHalf = newString.Substring(0, 16);
    string secondHalf = newString.Substring(16, 16);
    double firstHalfDouble = HexToDouble(firstHalf);
    double secondHalfDouble = HexToDouble(secondHalf);
    firstHalfDouble = Math.Round(firstHalfDouble, 6);
    secondHalfDouble = Math.Round(secondHalfDouble, 6);
    return new Tuple<double, double>(firstHalfDouble, secondHalfDouble);
  }
  
  public static double HexToDouble(string hex)
  {
   
    double res = 0;
    for (int i = 0; i < hex.Length; i++)
    {
   
      int dec = Convert.ToInt32(hex[i].ToString(), 16);
      res += dec * Math.Pow(16, -(i + 1));
    }
    return res;
  }
  
  public static string MakeString(double dow, DateTime date)
  {
   
    string newString = date.ToString("yyyy-MM-dd-");
    newString += dow.ToString("0.00");
    return newString;
  }
  
  public static string CreateMD5(string input)
  {
   
    // Use input string to calculate MD5 hash
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
   
      byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
      byte[] hashBytes = md5.ComputeHash(inputBytes);
      
      // Convert the byte array to hexadecimal string
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < hashBytes.Length; i++)
      {
   
        sb.Append(hashBytes[i].ToString("X2"));
      }
      return sb.ToString();
    }
  }
}

答案2:

using System;
using System.Security.Cryptography;
using System.Text;

public class Kata
{
   
  public static Tuple<double, double> GeoHashing(double dow, DateTime? date = null)
  {
   
    DateTime dt = date ?? DateTime.UtcNow.Date;
    string newString = makeString(dow, dt);
    newString = CreateMD5(newString);
    string firstHalf = newString.Substring(0, 16);
    string secondHalf = newString.Substring(16, 16);
    long firstHalfLong = Convert.ToInt64(firstHalf.Substring(0, 8), 16);
    double firstHalfDouble = (double)firstHalfLong / Math.Pow(16, 8);
    firstHalfDouble = Math.Round(firstHalfDouble, 6);
    long secondHalfLong = Convert.ToInt64(secondHalf.Substring(0, 8), 16);
    double secondHalfDouble = (double)secondHalfLong / Math.Pow(16, 8);
    secondHalfDouble = Math.Round(secondHalfDouble, 6);
    return new Tuple<double, double>(firstHalfDouble, secondHalfDouble);
  }
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值