从两个具有相同长度且具有完全相同的素因数集的数组中计算对
count the pairs from two arrays with same length which has exactly same set of prime Divisors
我正在尝试一个 codility 任务并遇到一个问题,给定两个非空数组 A 和 B 的 Z 整数,returns 位置 K 的数量,其中 A 的素因数[K]和B[K]完全一样
例如,给定:
A[0] = 15 B[0] = 75
A[1] = 10 B[1] = 30
A[2] = 3 B[2] = 5
函数应该return1,因为只有一对 (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}.
我应用的解决方案如下,通过尝试多个测试用例,我没有发现错误的答案,但在 codility 上它说答案不正确。
请帮我找出原因:
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;
}
}
2 个数具有相同的质因数这一事实并不意味着较大的数可以被较小的数整除,例如12=2*2*3 和 18=2*3*3.
我正在尝试一个 codility 任务并遇到一个问题,给定两个非空数组 A 和 B 的 Z 整数,returns 位置 K 的数量,其中 A 的素因数[K]和B[K]完全一样
例如,给定:
A[0] = 15 B[0] = 75
A[1] = 10 B[1] = 30
A[2] = 3 B[2] = 5
函数应该return1,因为只有一对 (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}.
我应用的解决方案如下,通过尝试多个测试用例,我没有发现错误的答案,但在 codility 上它说答案不正确。 请帮我找出原因:
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;
}
}
2 个数具有相同的质因数这一事实并不意味着较大的数可以被较小的数整除,例如12=2*2*3 和 18=2*3*3.