我正在尝试编码任务并遇到一个问题,给定Z个整数的两个非空数组A和B,返回A [K]和B [K]的素数除数的位置数K ]完全一样。
例如,给定:
A[0] = 15 B[0] = 75
A[1] = 10 B[1] = 30
A[2] = 3 B[2] = 5
函数应返回1,因为只有一对(15,75)具有相同的素数除数。
例如,给定:
N = 15 and M = 75, the prime divisors are the same: {3, 5};
N = 10 and M = 30, the prime divisors aren't the same: {2, 5} is not equal to {2, 3, 5};
N = 9 and M = 5, the prime divisors aren't the same: {3} is not equal to {5}.
我已应用的解决方案如下所示,在尝试多个测试用例时,我没有找到错误的答案,但在编码时,它说答案是不正确的。 请帮我找到原因:
public class CommonPrimeDivisors {
public static void main(String[] args) {
int A[] = new int[] { 175 };
int B[] = new int[] { 350 };
System.out.println(new CommonPrimeDivisors().solution(A, B));
}
public int solution(int A[], int B[]) {
int counter = 0;
for (int i = 0; i < A.length; i++) {
double max = Math.max(A[i], B[i]);
double min = Math.min(A[i], B[i]);
double remainder = (double) max / min;
if(remainder==(int)Math.ceil(remainder)){
if (min % remainder == 0) {
counter++;
}
}
}
return counter;
}
}
答案 0 :(得分:0)
2个数字具有相同的除数的事实并不意味着较大的数字可以被较小的除数整除,例如12 = 2 * 2 * 3和18 = 2 * 3 * 3.