[英]Closest divisible integer
Example.例子。
int a = 254;
int b = 25;
int c = (closest integer to `a` that is divisible by `b`)
How can I find the integer c
?我怎样才能找到 integer
c
? The result of that example is c = 250
.该示例的结果是
c = 250
。
There are two cases to consider:有两种情况需要考虑:
The closest integer that is less than or equal to a
:小于或等于
a
的最接近整数:
int c1 = a - (a % b);
The closest integer that is greater than a
:大于
a
的最接近整数:
int c2 = (a + b) - (a % b);
Then we need to check which is closer to a
and return that:然后我们需要检查哪个更接近
a
并返回:
int c;
if (a - c1 > c2 - a) {
c = c2;
} else {
c = c1;
}
So we could create a closestInteger()
method like this:所以我们可以像这样创建一个
closestInteger()
方法:
static int closestInteger(int a, int b) {
int c1 = a - (a % b);
int c2 = (a + b) - (a % b);
if (a - c1 > c2 - a) {
return c2;
} else {
return c1;
}
}
Example:例子:
System.out.println(closestInteger(254, 25));
System.out.println(closestInteger(9, 5));
Output:输出:
250
10
You have to check on both sides of a
.你必须检查
a
两边。 So we set 'c1' to the closest integer below (or equal to) a
, and c2
to be the closest integer above it.因此,我们将 'c1' 设置为低于(或等于)
a
的最接近的整数,并将c2
设置为高于其的最接近的整数。 Then compare the differences.然后比较差异。 If
c1
is closer to a
, set c
to c1
, otherwise c2
.如果
c1
更接近a
,则将c
设置为c1
,否则设置c2
。
int c1 = a - (a % b);
int c2 = c1 + b;
int c = a - c1 < c2 - a ? c1 : c2;
Closest below: int c = (a/b)*b;下面最接近: int c = (a/b)*b;
Since a and b are both int.因为 a 和 b 都是整数。 The division a/b will truncate to the nearest in value (10).
除法 a/b 将截断到最接近的值 (10)。 Then multiplying the result by b will give what you are looking for.
然后将结果乘以 b 将给出您要查找的内容。
Or或者
Closest either way:最接近的任一方式:
int c = Math.round((float)a/b))*b; int c = Math.round((float)a/b))*b;
Since a is cast as float the answer will be a float and rounding goes to the nearest int.由于 a 被转换为浮点数,因此答案将是浮点数,四舍五入到最近的整数。
If you don't mind going to the next highest integer when there is a tie (eg closest integer to 6 that is a multiple of 4 becomes 8 and not 4), then you can use this simple formula.如果您不介意在出现平局时转到下一个最高整数(例如,最接近 6 的整数是 4 的倍数变为 8 而不是 4),那么您可以使用这个简单的公式。 No comparisons are needed.
不需要比较。
int c = (a + b/2)/b * b; // closest integer to `a` that is divisible by `b`
All calculations are done with integer arithmetic (fractions are discarded).所有的计算都是用整数算术完成的(分数被丢弃)。
var nums = [0, 9, 26, 39]; for (var i=0 ; i<nums.length;i++){ const mod = nums[i]%3; if(mod!=0){ console.log("Closest Multiple: "+(3*Math.round(nums[i]/3))) }else{ console.log("Closest multiple: "+nums[i]) } }
static void checkDivisibility(int i,int j) {
int mod = i % j;
if (mod == 0) {
System.out.println(i+ " is divisible by 13 ");
} else {
if (mod >= 7) {
i = (i + (j - mod));
} else {
i = i - mod;
}
System.out.println(i+" is the nearest divisible ");
}
}
Call this method : checkDivisibility(40,13);调用此方法: checkDivisibility(40,13); Output : 39 is the nearest divisible
输出:39 是最近的可整除数
Closet number less than a int c1 = a - (a % b);壁橱数小于一个 int c1 = a - (a % b);
Closet number greater than a int c2 = a + (b - (a%b))壁橱数量大于一个 int c2 = a + (b - (a%b))
Now compare which one has less difference from a现在比较哪个与一个的差异较小
int d1 = a - c1; int d1 = a - c1;
int d2 = c2 - a; int d2 = c2 - a;
if(d1 > d2)//closest number is c2 if(d1 > d2)//最近的数是c2
else //closest number is c1 else //最近的数是c1
int actual; // for example 23
int divisibleBy; // for example 8
int result = actual + divisibleBy - 1 & -divisibleBy; // result is 24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.