【发布时间】:2020-06-03 21:30:58
【问题描述】:
问题:你将得到两个整数数组,并要求你确定满足以下两个条件的所有整数:
第一个数组的元素都是正在考虑的整数的因子 正在考虑的整数是第二个数组的所有元素的因子 这些数字被称为位于两个数组之间。您必须确定存在多少这样的数字。
例如:示例输入
2 3
2 4
16 32 96
样本输出
3
我的代码:
public static int getTotalX(int n, int m, List<Integer> a, List<Integer> b) {
int total=0,x=0,y=0;
for(int i=a.get(n-1);i<=b.get(0);i++)
{
for(int j=0;j<n;j++)
{
//to check if the elements in list 'a' can divide the integer.
if(i%a.get(j)==0)
{
y++;
}
}
//if every element in list a can divide the integer go forward
if(y==n)
{
for(int k=0;k<m;k++)
{
//to check if the elements of list 'b' is divisible by integer
if(b.get(k)%i==0)
{
x++;
}
}
y=0;
//if every element of 'b' is divisible by integer, count how many such integers are there
if(x==m)
{
total++;
x=0;
}
}
}
return total;
}
我的代码没有给出正确的解决方案,我不明白为什么。
【问题讨论】:
-
解释你输入的每一行是什么意思?
-
当你说你的代码没有给出正确的解决方案时,你能告诉我们它给出了什么解决方案吗?
-
你的问题有点不清楚,你能解释一下你的输入以及为什么你得到那个输出
-
是的!我很抱歉不清楚。对于上面提到的例子,我的输出是 1 而不是 3。
-
看来你的输入数组是按升序排序的,所以我没有得到你的第一个
for循环:不应该是``for (int i=a.get(0); i