比较多个数字之间的差异 [英] Compare difference between multiple numbers

查看:129
本文介绍了比较多个数字之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我有5个数字组成的数组:

The problem is that I have array with 5 numbers:

300 295 250 105 100 95

300 295 250 105 100 95

所需结果:阈值指定的差异最小的数字最多. 如果您无法理解:在示例中,门槛是5,而中奖号码是95,100,105-因为有3个号码彼此接近,而其他号码(295,300)只有2.

The result needed: the most numbers that have least difference specified by threshold. If you cant understand: in the example threshold is 5 and the winning set of numbers is 95,100,105 - because there are 3 numbers that are close to each other and the other set (295,300) is only 2.

我将尽力尽快给出更清晰的解释.

I will try to come up with more clear explanation soon.

推荐答案

我无法提供Javascript代码,但我建议:

I cannot give Javascript code, but I propose:

  • 对列表进行排序
  • 计算顺序差异
  • threshold以下的任何值四舍五入或裁剪为零,其他所有值均裁剪为一个
  • 寻找最长的连续零序列(行程编码)
  • Sort the list
  • Compute sequential differences
  • round or clip any values below threshold to zero, and all others to one
  • look for the longest continuous sequence of zeros (run-length encoding)
{95, 100, 105, 250, 295, 300}

--->  {5, 5, 145, 45, 5}

--->  {0, 0, 1, 1, 0}

这篇关于比较多个数字之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆