【问题标题】:Problem in "between two set" hackerrank problem“两组之间”hackerrank 问题中的问题
【发布时间】: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

标签: java algorithm


【解决方案1】:
private 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++) {
            if (i % a.get(j) == 0) {
                y++;
            }
        }
        if (y == n) {
            for (int k = 0; k < m; k++) {
                if (b.get(k) % i == 0) {
                    x++;
                }
            }
            if (x == m) {
                total++;
            }
        }
        // changes here
        y = 0;
        x = 0;
    }
    return total;
}

进步很大。你非常亲近。算法准确且高效。

只有一个错误:您在if 条件中重置了变量xy

如果条件不成立怎么办?然后变量永远不会重置,并且所有未来的计算都是在 xy 中的错误值上完成的。


喜欢 Java8?这是一个单行:

return (int) IntStream.rangeClosed(a.get(n - 1), b.get(0))
        .filter(i -> a.stream().filter(value -> i % value == 0).count() == a.size())
        .filter(i -> b.stream().filter(value -> value % i == 0).count() == b.size())
        .count();

【讨论】:

  • 你帮了大忙!感谢您的赞赏,我只是编码的初学者。
  • 对我来说这是一个不同的级别,但我想我也可以在那里看到解决方案,我只是不知道流。
【解决方案2】:

我在 Python 中的解决方案

def getTotal(a,b):
  consider_ineger=[]
  for i in range(a[len(a)-1],b[0]+1):
    is_factor=False

    for n in a:
        if i%n==0:
            is_factor=True
        else:
            is_factor=False
            break
    if is_factor:
        consider_ineger.append(i)

 print(consider_ineger)
 final=[]

 for i in consider_ineger:
    is_factor=False
    for n in b:
        if n%i==0:
            is_factor=True
        else:
            is_factor=False
            break
    if is_factor:
        final.append(i)
return len(final)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2022-09-25
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多