我怎样才能获得的5或10的下一个最高倍数倍数、最高

2023-09-11 04:56:20 作者:摆拍的孤寂

这是一个给定的我要根据一定的规则当中,因为我有一定​​的难度,描述他们,我会通过例子说明,以获得下一个最高数量:

 输入所需的输出
------- --------------
   0.08 0.1
   0.2 0.5
   5 10
   7 10
  99 100
 100 500
2345 5000
 

输出应该在一定意义上的5或10次高的多。

我希望这是可以理解的;如果没有,让我知道。

的实施将是在Java和投入将是积极的秒。

解决方案

 函数top5_10(X){
  变种10 = Math.pow(10,Math.ceiling(Math.ln(x)的/Math.LN10)));
  如果(10→10 * x)的{10 = 10/10; }
  否则,如果(十大< = X){10 = 10 * 10; }
  返回X< 10/2? 10分/ 2:十;
}
 
记一次艰难的内网漫游第三期 我是如何拿到内网最高权限的

或者是这样的: - )

From a given double I want to get the next highest number according to some rules which, since I have some difficulty describing them, I will illustrate by examples:

Input      Desired output
-------    --------------
   0.08         0.1
   0.2          0.5
   5           10
   7           10
  99          100
 100          500
2345         5000

The output should be in some sense the 'next highest multiple of 5 or 10'.

I hope this is understandable; if not, let me know.

The implementation will be in java and input will be positive doubles.

解决方案

function top5_10 (x) {
  var ten = Math.pow(10, Math.ceiling(Math.ln(x)/Math.LN10)));
  if (ten > 10 * x) { ten = ten / 10; }
  else if (ten <= x) { ten = 10 * ten; }
  return x < ten / 2 ? ten / 2 : ten;
}

or something like this :-)