猿问

检查数组是否为235

我想检查数组是否为235。Is235 是一个数组,其中有一个可被 2 整除的整数、另一个可被 3 整除的整数和第三个可被 5 整除的整数。数组中的其他整数在与这些整数相加时不能被 2、3 或 5 整除能被 2、3 和 5 整除的数应等于数组中元素的总数。如果数组为235,则返回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 变量无法像我教的那样计算整数。


慕哥9229398
浏览 137回答 3
3回答

慕容3067478

试试这个,我测试了它并且它有效,你应该使用余数运算符%:public class Array {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] arr = {2, 3, 5, 7, 11};&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(is235Array(arr));&nbsp; &nbsp; }&nbsp; &nbsp; public static int is235Array(int[] a) {&nbsp; &nbsp; &nbsp; &nbsp; int countOne = 0;&nbsp; &nbsp; &nbsp; &nbsp; int countTwo = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i : a) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countOne++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{countTwo++;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (countOne + countTwo != a.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }else{return 1;}&nbsp; &nbsp; }}

隔江千里

检查数组是否为 235。is235Array。public class Array{static int is235Array(int[] a){&nbsp; &nbsp; int countNonMultiples = 0;&nbsp; &nbsp; int countMultiplesOfTwo = 0;&nbsp; &nbsp; int countMultiplesOfThree = 0;&nbsp; &nbsp; int countMultiplesOfFive = 0;&nbsp; &nbsp; for (int i : a){&nbsp; &nbsp; &nbsp; &nbsp; if(i % 2 == 0 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countMultiplesOfTwo++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(i % 3 == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countMultiplesOfThree++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(i % 5 == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countMultiplesOfFive++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(i % 2 != 0 && i % 3 != 0 && i % 5 != 0 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countNonMultiples++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(countMultiplesOfTwo + countMultiplesOfThree + countMultiplesOfFive + countNonMultiples != a.length){&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; return 1;}public static void main(String[] args) {&nbsp; &nbsp; int[] arr = {7,2,7,2,7,2,7,2,3,7,7};&nbsp; &nbsp; System.out.println(is235Array(arr));}}

LEATH

当你想比较一个整数是否可以被一个数字整除时,你应该使用余数运算符所以这是代码: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;}    }}另请注意,没有必要编写 2nd,for loop因为它根本没有必要,而且如果您可以使用单个 for 循环完成任务,这是一种不好的做法。另外如上所述,问题的答案for-each loop是, a比常规方法[性能方面]更好for loop,因此使用 afor-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;}    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答