【问题标题】:Check if array Is235检查数组是否为 Is235
【发布时间】:2019-08-28 05:10:32
【问题描述】:

我想检查一个数组是否为 Is235。 Is235 是一个数组,其中包含一个可被 2 整除的整数、另一个可被 3 整除的整数和第三个可被 5 整除的整数。数组中的其他整数在添加到整数时不能被 2、3 或 5 整除可被 2、3 和 5 整除应该等于数组中元素的总数。如果数组 Is235,则返回 1,否则返回 0。请注意,数组不能包含负整数或零。我只想以蛮力的方式解决这个问题,提前感谢您的帮助。 我的错误尝试 -

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int n = a.length;
        int countOne = 0;
        int countTwo = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] / 2 == 0 || a[i] / 3 == 0 || a[i] / 5 == 0) {
                countOne++;
            }
        }
        for (int j = 0; j < a.length; j++) {
            if (a[j] / 2 != 0 || a[j] / 3 != 0 || a[j] / 5 != 0) {
                countTwo++;
            }
        }
        if (countOne + countTwo != n) {
            return 0;
        }
        return 1;
    }
}

我的 countOne 和 countTwo 变量无法像我教的那样计算整数。

【问题讨论】:

  • 我投票结束这个问题,因为这不是一个“为我做作业”的网站
  • 我在添加我的代码时遇到了问题,它现在在那里。谢谢。
  • (a == 0 || b == 0) 的反义词是(a != 0 &amp;&amp; b != 0)。第二个循环中的if 语句在应该使用&amp;&amp; 时使用||
  • a / 2 == 0 不是测试 a 是否被 2 整除。您需要使用 % 运算符,而不是 / 运算符来测试它.
  • @ScaryWombat 如果a[j] 为1,则结果为假。

标签: java arrays


【解决方案1】:

试试这个,我测试了它,它可以工作,你应该使用余数运算符%

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int countOne = 0;
        int countTwo = 0;

        for (int i : a) {
            if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
                countOne++;
            }else{countTwo++;}
        }

        if (countOne + countTwo != a.length) {
            return 0;
        }else{return 1;}

    }
}

【讨论】:

  • @Tunde 欢迎您,您能否也接受我的回答作为正确答案?
  • 抱歉我的回复晚了。请问我如何接受一个正确的答案?
  • 您可以通过点击下方的“正确”标志来接受它,您可以在下方对我的回答投反对票@Tunde
【解决方案2】:

当你想比较一个整数是否能被一个数字整除时,你应该使用remainder operator

代码如下:

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int n = a.length;
        int countOne = 0;
        int countTwo = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 == 0 || a[i] % 3 == 0 || a[i] / 5 == 0) {
                countOne++;
            }else{countTwo++;}
        }

        if (countOne + countTwo != n) {
            return 0;
        }else{return 1;}

    }
}

另外请注意,没有必要写第二个for loop,因为它根本没有必要,如果您可以使用单个 for 循环来完成任务,这是一种不好的做法。

还提到了问题的answerfor-each loop 比常规for loop 更好[性能方面],因此使用for-each loop 会变成:

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int countOne = 0;
        int countTwo = 0;

        for (int i : a) {
            if (i % 2 == 0 || i % 3 == 0 || i / 5 == 0) {
                countOne++;
            }else{countTwo++;}
        }

        if (countOne + countTwo != a.length) {
            return 0;
        }else{return 1;}

    }
}

【讨论】:

    猜你喜欢
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 2022-01-25
    • 2016-05-19
    • 2011-08-01
    • 2014-05-16
    相关资源
    最近更新 更多