亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查數組是否為235

檢查數組是否為235

慕哥9229398 2023-06-21 13:25:01
我想檢查數組是否為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 變量無法像我教的那樣計算整數。
查看完整描述

3 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

試試這個,我測試了它并且它有效,你應該使用余數運算符%:


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;}


    }

}


查看完整回答
反對 回復 2023-06-21
?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

檢查數組是否為 235。is235Array。


public class Array{


static int is235Array(int[] a){


    int countNonMultiples = 0;

    int countMultiplesOfTwo = 0;

    int countMultiplesOfThree = 0;

    int countMultiplesOfFive = 0;


    for (int i : a){

        if(i % 2 == 0 ){

            countMultiplesOfTwo++;

        }

        if(i % 3 == 0){

            countMultiplesOfThree++;

        }

        if(i % 5 == 0){

            countMultiplesOfFive++;

        }

        if(i % 2 != 0 && i % 3 != 0 && i % 5 != 0 ){

            countNonMultiples++;

        }

    }


    if(countMultiplesOfTwo + countMultiplesOfThree + countMultiplesOfFive + countNonMultiples != a.length){

        return 0;

    }

    return 1;

}


public static void main(String[] args) {

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


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

}

}


查看完整回答
反對 回復 2023-06-21
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

當你想比較一個整數是否可以被一個數字整除時,你應該使用余數運算符

所以這是代碼:


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;}


? ? }

}


查看完整回答
反對 回復 2023-06-21
  • 3 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號