乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      求 1 + 2 + 3 + ... + n

       小樣樣樣樣樣樣 2022-10-14 發(fā)布于北京

      求 1 + 2 + 3 + ... + n,要求不能使用乘除法、for、while、if、else、switch、case 等關(guān)鍵字及條件判斷語句 A ? B : C


      解題思路

      需要利用邏輯與的短路特性來實(shí)現(xiàn)遞歸的終止,當(dāng) n == 0 時,(n > 0) && ((sum += Sum_Solution(n - 1)) > 0) 只執(zhí)行前面的判斷,為 false,然后直接返回 0;當(dāng) n > 0 時,執(zhí)行 sum += Sum_Solution(n - 1),實(shí)現(xiàn)遞歸計(jì)算 Sum_Solution(n)

      public class Solution {
          public int Sum_Solution(int n) {
              int sum = n;
              boolean flag = (n > 0) && (sum += Sum_Solution(n - 1)) > 0;
              return sum;
          }
      }
      

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多