Board logo

標題: 402-尾端遞迴階乘計算 [打印本頁]

作者: 許承鴻    時間: 2018-7-24 18:26     標題: 402-尾端遞迴階乘計算

import java.util.Scanner;
public class JPD04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        
    ...



    }
   

}
作者: 張閎鈞    時間: 2018-8-7 20:05

  1. import java.util.Scanner;
  2. public class JPD04 {
  3.              static Scanner keyboard = new Scanner(System.in);
  4.             public static void main(String args[]) {
  5.                     System.out.print("Inpu n <0<=n<=16>:");
  6.                 int a=keyboard.nextInt();
  7.                 int b=1;
  8.                 int g=f(a,b);
  9.                
  10.                 System.out.print(f(a,b));


  11.             }
  12.             
  13.             static int f(int a,int b)
  14.             {

  15.                     if(a==1)
  16.                     {
  17.                             return b;
  18.                     }
  19.                     else
  20.                     {
  21.                             return f(a-1,a*b);
  22.                     }
  23.             }
  24. }
複製代碼

作者: 巫晉宇    時間: 2018-8-7 20:09

package ghh;

import java.util.Scanner;

public class sgg {
         static Scanner k = new Scanner(System.in);
        public static void main(String[] args){
               
                int w=k.nextInt();
                int y=f(w,1);
       System.out.print(y);
               
        }
        static int f(int w,int e)
        {
                if(w==0)
                                {
                                return w;       
                                }
                else
                {
                        return f(w-1,w*e);
                }
        }

}
作者: 巫沛庭    時間: 2018-8-7 20:13

  1. import java.util.Scanner;
  2. public class JPA04 {
  3.     static Scanner keyboard = new Scanner(System.in);
  4.     public static void main(String args[]) {
  5.         
  6.     System.out.print("Input n(0<=n<=16)");
  7.     int num=keyboard.nextInt();
  8.     int g=f(num,1);
  9.     System.out.print(num+"的階乘: "+g);
  10.     }
  11.     static int f(int k,int r)
  12.     {
  13.             if(k==0)
  14.             {
  15.                     return r;
  16.             }
  17.             else{
  18.                     return f(k-1,r*k);
  19.             }
  20.     }

  21. }
複製代碼





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