Board logo

標題: 遞迴函式 - 計算總合 [打印本頁]

作者: tonyh    時間: 2013-1-19 17:00     標題: 遞迴函式 - 計算總合

本帖最後由 tonyh 於 2013-1-19 17:14 編輯

利用函數遞迴法, 分別計算 1加到10 以及 1加到100的總合.
  1. public class ch56
  2. {
  3.    public static void main(String args[])
  4.    {
  5.        System.out.println("1+2+3+...+10="+total(10));
  6.        System.out.println("1+2+3+...+100="+total(100));
  7.    }
  8.    
  9.    public static int total(int n)
  10.    {
  11.        if(n<=1)
  12.            return 1;
  13.        else
  14.            return n+total(n-1);
  15.    }
  16. }
複製代碼

作者: 黃博鴻    時間: 2013-1-19 17:09

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

作者: t2364705    時間: 2013-1-19 17:10

  1. public class ch56
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         System.out.println("1+2+3+...+10="+total(10));
  6.         System.out.println("1+2+3+...+100="+total(100));
  7.     }

  8.     public static int total(int n)
  9.     {
  10.         if(n<=1)
  11.             return n;
  12.         else
  13.             return n+total(n-1);
  14.     }
  15. }
複製代碼

作者: 劉漢文    時間: 2013-1-19 17:12

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

作者: t3742238    時間: 2013-1-19 17:13

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

作者: 蔡昀佑    時間: 2013-1-19 17:25

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

作者: 粘靖瑜    時間: 2013-1-19 17:25

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

作者: 尤泓鈞    時間: 2013-1-19 17:26

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

作者: lon    時間: 2013-1-19 17:26

  1. public class ch56{
  2.     public static void main(String args[]){
  3.     System.out.println("1+2+3+...+100="+total(100));
  4.     }
  5.     public static int total(int n){
  6.     if(n<=1)
  7.     return n;
  8.     else
  9.     return n+total(n-1);
  10.     }
  11. }
複製代碼

作者: 粘靖瑜    時間: 2013-1-19 17:28

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





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