第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

檢查數(shù)組是否為235

檢查數(shù)組是否為235

慕哥9229398 2023-06-21 13:25:01
我想檢查數(shù)組是否為235。Is235 是一個(gè)數(shù)組,其中有一個(gè)可被 2 整除的整數(shù)、另一個(gè)可被 3 整除的整數(shù)和第三個(gè)可被 5 整除的整數(shù)。數(shù)組中的其他整數(shù)在與這些整數(shù)相加時(shí)不能被 2、3 或 5 整除能被 2、3 和 5 整除的數(shù)應(yīng)等于數(shù)組中元素的總數(shù)。如果數(shù)組為235,則返回1,否則返回0。請(qǐng)注意,數(shù)組不能包含負(fù)整數(shù)或零。我只想以暴力方式解決這個(gè)問(wèn)題,提前感謝您的幫助。我的錯(cuò)誤嘗試——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 變量無(wú)法像我教的那樣計(jì)算整數(shù)。
查看完整描述

3 回答

?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

試試這個(gè),我測(cè)試了它并且它有效,你應(yīng)該使用余數(shù)運(yùn)算符%:


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


    }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-21
?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊

檢查數(shù)組是否為 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));

}

}


查看完整回答
反對(duì) 回復(fù) 2023-06-21
?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

當(dāng)你想比較一個(gè)整數(shù)是否可以被一個(gè)數(shù)字整除時(shí),你應(yīng)該使用余數(shù)運(yùn)算符

所以這是代碼:


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


? ? }

}

另請(qǐng)注意,沒(méi)有必要編寫 2nd,for loop因?yàn)樗緵](méi)有必要,而且如果您可以使用單個(gè) for 循環(huán)完成任務(wù),這是一種不好的做法。


另外如上所述,問(wèn)題的答案for-each loop是, a比常規(guī)方法[性能方面]更好for loop,因此使用 afor-each loop會(huì)變成:

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


? ? }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-21
  • 3 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)