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

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

為什么這個(gè)函數(shù)總是返回 0 或 1

為什么這個(gè)函數(shù)總是返回 0 或 1

有只小跳蛙 2022-06-04 14:56:53
我正在嘗試學(xué)習(xí)一些關(guān)于遞歸的知識,所以我正在嘗試做一些練習(xí),但現(xiàn)在我有點(diǎn)卡住了,因?yàn)槲也恢罏槭裁催@個(gè)函數(shù)總是返回 1 或 0 我試圖計(jì)算數(shù)量11 在 int 數(shù)組中的出現(xiàn)。public class Uloha06 {public static int count = 0;public static int array11(int[] nums, int index){    if(index<nums.length){        if(nums[index]==11)            count+=1;        index++;        array11(nums,index);        if(index<nums.length)            return index;    }     return count;}public static void main(String[] args) {    int array11[]={11,1,2,36,11};    System.out.println(array11(array11, 0));    }}
查看完整描述

3 回答

?
湖上湖

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

它返回0一個(gè)空數(shù)組和1一個(gè)非空數(shù)組。您看到的結(jié)果來自index,而不是count您期望的結(jié)果。


我會在不涉及領(lǐng)域的情況下編寫它。


public int m(int[] nums, int index, int count) {

    return index < nums.length ?

            m(nums, index + 1, nums[index] == 11 ? ++count : count) :

            count;

}

或(@Pshemo 在評論中建議)


public int m(int[] nums, int index) {

    return index < nums.length ?

            (nums[index] == 11 ? 1 : 0) + m(nums, ++index) :

            0;

}


查看完整回答
反對 回復(fù) 2022-06-04
?
揚(yáng)帆大魚

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

您返回索引。標(biāo)記您的問題:


public class Uloha06 {

    public static int count = 0;

    public static int array11(int[] nums, int index){

        if(index<nums.length){

             if(nums[index]==11)

                 count+=1;

             index++;

             array11(nums,index);

             /// booom here index is 1 but you want count

             //if(index<nums.length)

             //    return index;

         } 

         return count;

     }

     public static void main(String[] args) {

        // TODO Auto-generated method stub

        int array11[]={11,1,2,36,11};

        System.out.println(array11(array11, 0));

     }


查看完整回答
反對 回復(fù) 2022-06-04
?
30秒到達(dá)戰(zhàn)場

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

這應(yīng)該工作


public static int array11(int[] nums, int index){


if(index < 0){

    return 0;}


 else if (nums [index] == 11)

 { 

        return (array11(nums, index-1) + 1);


 }

   else {

      return  array11(nums, index-1);


   }



查看完整回答
反對 回復(fù) 2022-06-04
  • 3 回答
  • 0 關(guān)注
  • 122 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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