close

401

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        System.out.print("Input n (0 <= n <= 16):");
        int n = keyboard.nextInt();
        while(n!=999){
            if(n!=999){ //若 n 不為 999 則打印其內容
                System.out.printf("%d 的階乘 = %d\n",n,factorial(n));
            }
            System.out.print("Input n (0 <= n <= 16):");
            n = keyboard.nextInt();
        
        }

    }
    
    static int factorial(int y){ //自訂一個遞迴方法
        if(y==0){    //若傳入 0 ,則返回 1
            return 1;
        }else{       // 傳入任意數字,則將他 遞迴的 乘上本身減1,
            return (y * factorial(y-1));
        }
    }

}

402

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        
        System.out.print("Input n (0 <= n <= 16):");
        int n = keyboard.nextInt();
        int sum = 1; // 題目規定要另外傳入參數來累計遞迴的答案
        while(n!=999){
            System.out.printf("%d 的階乘(尾端遞迴) = %d\n",n,fac(n,sum));
            System.out.printf("%d 的階乘(迴圈) = %d\n",n,loop(n));
            System.out.print("Input n (0 <= n <= 16):");
            n = keyboard.nextInt();
        }


    }
    
    
    static int fac(int x, int sum){ //尾端遞迴:在返回值時調用自己
        if (x == 0) return  sum;     //若 x 為 0,返回 1
        else return fac(x-1,x*sum);//若 x 不為 0,則將 x-1 再次帶入遞迴
    }
    
    static int loop(int y){
        int sum = 1;
        while(y>0){  //迴圈
            sum = y * sum;
            y = y -1; //每迴圈一次,減少1,直到為 0 停止
        }
        return sum;
    }






}

403

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
       System.out.print("Input m:");
       int sum =1; //設一變數為累積遞迴的答案
       int m = keyboard.nextInt();       
       while(m != 999){ //若 m 為 999 時,則跳出迴圈
              System.out.print("Input n:"); //由於輸入 m 後就要判定是否運行迴圈,所以 n 要放入迴圈之中
              int n = keyboard.nextInt();
              System.out.printf("Ans (尾端遞迴) : %d\n",fac(m,n,sum));
              System.out.printf("Ans (迴圈) : %d\n",loop(m,n));
              System.out.print("Input m:");
              m = keyboard.nextInt();
                      

        }
    }
    static int fac (int x, int y,int sum){ //尾端遞迴
        if (y == 1) return x * sum;        // y = 1 時就停止,=0的話會多乘一次
        else return fac(x,y-1,x*sum);  //尾端遞迴,在結尾呼叫自己
    }
    static int loop (int a, int b){ //迴圈
        int sum =1;                 //設一變數存放最後值
        while (b > 0){              //若大於 0 就繼續運行
            sum = sum * a;          //每次迴圈乘上一次 a
            b = b - 1;              //每次迴圈,次方數減少 1
        }
        return sum;                 //返回最後值
    }
    


}

404

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        
      System.out.print("Input m: ");
      int m = keyboard.nextInt();
      while(m!=999){
            System.out.print("Input n: ");
            int n = keyboard.nextInt();
            System.out.printf("最大公因數為:%d\n",gcd(m,n)); //呼叫自訂函數 gcd
            System.out.print("Input m: ");
            m = keyboard.nextInt();
      }


    }
    static int gcd(int x, int y){
        if (x % y == 0){  //若 餘數為0 則返回 y
            return y;
        }else{            //若 餘數不為0 則輾轉相除
            return gcd(y,x%y);
        }
    }
}

405

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        System.out.print("Input the number n: ");
        int n = keyboard.nextInt();
        if(n>=1) //題目要求 n >=1
            System.out.printf("Ans: %d",sum2(n));
    }
    
    static int sum2(int x){
        if(x == 1) return 2; //題目要求,若sum2 = 1,則返回 2
        else{
            return sum2(x -1) + 2 * x; //依照題目公式
        }
    }

}

406

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
      
      System.out.print("Input a a string: ");
      String x = keyboard.nextLine(); //讀取整行字串
      System.out.printf("%s has %d As\n",x,countA(x));
      System.out.print("Input a a string: ");
      x = keyboard.nextLine();
      System.out.printf("%s has %d As\n",x,countA(x));

    }
    
    public static int countA(String str) {
          if(str.equals("")) return 0; //若字串沒有內容,則返回0
          else if(str.substring(0,1).equals("A"))//若字串第1個字為A,
                return 1 + countA(str.substring(1)); //則返回 1 + 從字串第2個字開始的內容(ex.若為"Apple" 則返回 1 + "pple")
          else return countA(str.substring(1)); //若字串第1個字不為A,則返回第一個字元後之後的所有內容
                
      
 
      }
}

//string.equals(x) 比較字串與x是否相同
//string.substring(x,y) 取出字串中 第 x 到第 y 個字元的字串
//string.substring(x)  取出字串中 第 x 個字元之後的字串

407

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        String s; 
        System.out.print("Input a string of numbers: ");
        s = keyboard.nextLine();
        System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
        System.out.printf("迴圈:%d\n", sumLoop(s, 0));
        System.out.print("Input a string of numbers: ");
        s = keyboard.nextLine();
        System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
        System.out.printf("迴圈:%d\n", sumLoop(s, 0));
    }
    
   // Integer.parseInt(String s)  將字串解析為十進位整數
   static int sumTail(String x, int y){
          if (x.equals("")) return y; //若字串已經無內容,則返回 y
          else{
                 y = y + Integer.parseInt(x.substring(0,1)); //將第一個整數取得,加總到y上
                 return sumTail(x.substring(1), y);
          }
   }
   // String.length() 取得字串長度
   static int sumLoop(String a, int b){
          for(int i =0;i<a.length();i++){
                 b = b + Integer.parseInt(a.substring(i,i+1)); //將第一個整數取得,加總到b上
          }
          return b;
          
   }

}

408

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        String s, c; 
        System.out.print("Input a string: ");
        s = keyboard.nextLine();
        System.out.printf("%s\n", reverse(s));
        System.out.print("Input a string: ");
        s = keyboard.nextLine();
        System.out.printf("%s\n", reverse(s));
    }
    
    static String reverse(String x){
        
        if (x.equals("")) return ""; //若字串已無內容,則返回空值
        else return reverse(x.substring(1))+x.substring(0,1);//返回 "遞迴(第二個字元開始的字串)",在尾端加上第一個字元
    }
}

409

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        String s, c; 
        System.out.print("Input a string: ");
        s = keyboard.nextLine();
        System.out.print("Input a character: ");
        c = keyboard.nextLine();
        System.out.printf("%s\n", removeChar(s, c));
        System.out.print("Input a string: ");
        s = keyboard.nextLine();
        System.out.print("Input a character: ");
        c = keyboard.nextLine();
        System.out.printf("%s\n", removeChar(s, c));
    }
    
    static String removeChar(String x, String y){
        if (x.equals("")) return "";  //若該字串已無內容,則返回空值
        else if (x.substring(0,1).equals(y)) return removeChar(x.substring(1),y); //若第一個字元等於要移除的字元,則返回removeChar(第二個字元開始, 要移除的字元)
        else return x.substring(0,1) + removeChar(x.substring(1),y); //若第一個字元有內容且不等於要移除的字元,則返回 (第一個字元 + removeChar(第二個字元開始, 要移除的字元))
    }
}

410

import java.util.Scanner;
public class JPA04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        String s, c1, c2; 
        System.out.print("Input a string: ");
        s = keyboard.nextLine();
        System.out.print("Input a character: ");
        c1 = keyboard.nextLine();
        System.out.print("Input another character: ");
        c2 = keyboard.nextLine();
        System.out.printf("%s\n", replace(s, c1, c2));
    }
    
    static String replace(String a, String b, String c){
        if (a.equals("")) return ""; //若字串已無內容,則返回空值
        else if(a.substring(0,1).equals(b)){ //若第一個字元等於要被替換的字元
            return c + replace(a.substring(1),b,c);//返回 (替換上的字元 + replace(第二個字元開始,要被替換的字元,替換上的字元))
        }
        else{
            return a.substring(0,1) + replace(a.substring(1),b,c);//返回 (第一個字元 + replace(第二個字元開始,要被替換的字元,替換上的字元))
        }
    }
}
arrow
arrow
    全站熱搜

    ivankao 發表在 痞客邦 留言(0) 人氣()