Board logo

標題: 遞迴函式 (一) - 計算總和 [打印本頁]

作者: tonyh    時間: 2019-1-25 11:25     標題: 遞迴函式 (一) - 計算總和

本帖最後由 tonyh 於 2019-7-3 21:21 編輯

運用遞迴函式,計算從1到某個數的正整數之和。

[attach]5780[/attach]
  1. public class Ch50 {       
  2.         static int total(int n)
  3.         {
  4.                 if(n==1)    //邊界值
  5.                         return 1;
  6.                 else
  7.                     return n+total(n-1);
  8.         }
  9.         /*
  10.              total(5)
  11.              =5+total(4)
  12.              =5+4+total(3)
  13.              =5+4+3+total(2)
  14.              =5+4+3+2+total(1)
  15.              =5+4+3+2+1
  16.         */
  17.         public static void main(String[] args)
  18.         {
  19.                 System.out.println("1+2+...+5="+total(5));
  20.                 System.out.println("1+2+...+101="+total(101));
  21.                 System.out.println("1+2+...+257="+total(257));
  22.         }
  23. }
複製代碼

作者: 楊于暄    時間: 2019-1-25 11:45

  1. public class Ch32
  2. {
  3.         static int total(int n)
  4.         {
  5.                 if(n==1)
  6.                         return 1;
  7.                 else
  8.                         return n+total(n-1);
  9.         }
  10.         public static void main(String[] args)
  11.         {
  12.                 System.out.println("1+2+...+5="+total(5));
  13.                 System.out.println("1+2+...+101="+total(101));
  14.                 System.out.println("1+2+...257="+total(257));
  15.         }
  16. }
複製代碼

作者: 楊貳鈞    時間: 2019-1-25 11:47

  1. public class Ch02
  2. {
  3.         static int total(int n)
  4.         {
  5.                 if(n==1)
  6.                   return 1;
  7.                 else
  8.                   return n+total(n-1);
  9.         }
  10.         public static void main(String[] args)
  11.         {
  12.             System.out.println("1+2+3+4+5="+total(5));
  13.             System.out.println("1+...+101="+total(101));
  14.             System.out.println("1+...+257="+total(257));
  15.         }
  16. }
複製代碼

作者: 孫焌仁    時間: 2019-1-25 11:47

  1. public class Ch10
  2. {
  3.         static int total(int n)
  4.         {
  5.                  if(n==1)
  6.                          return 1;
  7.                  else
  8.                          return n+total(n-1);
  9.         }
  10.        
  11.         public static void main(String args[])
  12.     {
  13.                 System.out.println("1+2+...+5133="+total(5133));
  14.                 System.out.println("1+2+...+6734="+total(6734));
  15.                 System.out.println("1+2+...+15343="+total(15343));
  16.     }
  17. }
複製代碼

作者: 趙一鳴    時間: 2019-1-25 11:48

  1. public class Ch11
  2. {
  3.     static int plussNum(int i)
  4.     {
  5.             if(i==1)
  6.                     return 1;
  7.             else
  8.                     return i+plussNum(i-1);
  9.     }
  10.         public static void main(String[] args)
  11.         {
  12.                 System.out.println("1+2+...+5:"+plussNum(5));
  13.                 System.out.println("1+2+...+101:"+plussNum(101));
  14.                 System.out.println("1+2+...+199:"+plussNum(199));
  15.         }

  16. }
複製代碼

作者: 林育鋐    時間: 2019-1-25 11:49

  1. public class Ch15 {
  2.         static int f1 (int a)
  3.         {
  4.                 if(a==1)
  5.                         return 1;
  6.                 else
  7.                         return a+f1(a-1);
  8.         }

  9.         public static void main(String[] args) {
  10.                  System.out.println("1+2+3+.....+100="+f1(100));
  11.                  System.out.println("1+2+3+.....+599="+f1(599));
  12.         }

  13. }
複製代碼

作者: 王騰立    時間: 2019-1-25 11:55

  1. import java.util.Scanner;
  2. public class Ch
  3. {
  4.         static int total(int n)
  5.         {
  6.               if(n==1)
  7.                        return 1;
  8.               else
  9.                        return n+total(n-1);
  10.         }
  11.         public static void main(String[] args)
  12.         {
  13.             System.out.println("1+2+...+5="+total(5));
  14.             System.out.println("1+2+...+101="+total(101));
  15.             System.out.println("1+2+...257="+total(257));
  16.         }
  17. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2