我想知道一个数字在另一个数字内适合多少次。例如:
24/2 = 12
12/2 = 6 //它适合6次......
6/2 = 3 //它适合3次......
我做了什么
while (done == false) {
if(start == 0) {
resultDivide = a[0] / a[1];
start = 1;
counter ++;
} else {
tempresult = resultDivide / a[1];
}
}
答案 0 :(得分:2)
在您的示例中,a
为24,b
为3
if(a == 0){
//ans is infinity
}
else {
int copy = a;
int times = 0;
while(copy % b == 0){
copy /= b;
++times;
}
}
答案 1 :(得分:1)
这是一种方法:
public static int numDiv(int a, int b) {
if (b < 2) // nonsense value
throw new IllegalArgumentException();
int result = 0;
for (; a % b == 0; a /= b)
result++;
return result;
}
答案 2 :(得分:0)
while ((num1 % num2 == 0) && num2 != 0)
{
num1 /= num2;
times++;
}