2016-09-14 1 views
-1

現在、5つの方法などを行う再帰的コードを作成しています。私の再帰的メソッドは、2の累乗から2の累乗までのmain()を返して出力します.Xはコマンドライン引数の整数です。Math.powを使用せずに再帰的な方法(2の累乗を2のx乗に戻す)

私はmath.powプロセスを実行できますが、Math.Powを使用せずにどのように行うのですか?

マイコード(問題はpowerCountメソッドです)。

public class TestRun { 
/* 
* The "main" Method Starts The Program. 
* @param args (commandline arguments) are printed to the screen 
*/ 

    public static void main (String[]args) { 

    //initialize variables 
     int num = 0; 
     String result = ""; 
     /*********************************** 
    * @ Try-Catch Checks for valid input 
     * **********************************/ 
     try{ 
     if(args.length == 0) 
     { 
      System.out.println("ERROR:NO INPUT FOUND."); 
     }//end of if 
     else 
     { 
     //convert 1st commandline argument from string to integer  
      num = Integer.parseInt(args[0]); 
     //method call to 1st method 
      result = rowOfAsterisks(num); 
     //display output of 1st method to screen  
      System.out.println(result); 
     //method call to 2nd method 
      result = forwardCount(num); 
     //display output of 2nd method to screen  
      System.out.println(result); 
     //method call to 3rd method 
      result = reverseCount(0, num); 
     //display output of 3rd method to screen  
      System.out.println(result); 
     //method call to 4th method 
      Integer result2 = addCount(0,num); 
     //display output of 4th method to screen  
      System.out.println(result2); 
     //method call to 5th method  
      result2 = powerCount(1,(int)Math.pow(2,num)); 
     //display output of 5th method to screen  
      System.out.println(result2); 
     }//end of else 
     }//end of try 
     catch(NumberFormatException e) 
     { 
     System.out.print("ERROR: Please input an integer."); 
     } 
    }//end of main 

    /************************** 
    * Writes x asterisk 
    * @param num is input of the user and will determine the number of asterisks 
    * @return x asterisk 
    ***************************/ 
    public static String rowOfAsterisks(int num) 
    { 
     //base case 
     if(num ==1){ 
     return "*"; 
     } 
     //recursive case 
     else{ 
     return "*" + rowOfAsterisks(num-1); 
     }//end of else 
    }//end of method1 

    /***************************** 
    * Counts from number to zero. 
    * @param num is the input of the user or the argument 
    * @return string num 
    *****************************/ 
    public static String forwardCount(int num) 
    { 
     //base case 
     if(num==0){ 
     return num + ", "; 
     } 
     //recursive case 
     else{ 
     return num + ", " + forwardCount(num-1); 
     }//end of else 
    }//end of method 2 

    /*************************************************** 
    * Counts from zero to number. 
    * @param initialNum is the input/initial integer 
    * @param finalNum is the final integer 
    * @return initialNum and finalNum as a String. 
    ****************************************************/ 
    public static String reverseCount(int initialNum, int finalNum) 
    { 
     //base case 
     if(initialNum == finalNum){ 
     return initialNum + ", "; 
     } 
     //recursive case 
     else{ 
     return initialNum + ", " + reverseCount(initialNum +1, finalNum); 
     }//end of else 
    }//end of method 3 

    /***************************************************** 
    * Adds the number from 0 to number and returns the sum 
    * @param initialNum is the input/initial integer 
    * @param finalNum is the final integer 
    * @return initialNum and finalNum as a String(again) 
    ****************************************************/ 
    public static int addCount(int initialNum, int finalNum) 
    { 
     //base case 
     if(initialNum == finalNum){ 
     return initialNum; 
     } 
     //recursive case 
     else{ 
     return initialNum + addCount(initialNum + 1, finalNum); 
     }//end of else 
    }//end of method4 

    /********************************************************** 
    * Multiply number by the power of 2 and returns the product 
    * @param initialNum is the input/initial integer 
    * @param finalNum is the final integer 
    * @return initialNum and finalNum as an Integer 
    ***********************************************************/ 
    public static int powerCount(int initialNum, int finalNum) 
    { 
     //base case 
     if(initialNum == finalNum){ 
     return initialNum; 
     } 
     //recursive case 
     else{ 
     return initialNum + powerCount(initialNum *2, finalNum); 
     }//end of else 
    }//end of method5 
}//end of class 

ここpowerCount方法であって、

public static int powerCount(int initialNum, int finalNum) 
    { 
     //base case 
     if(initialNum == finalNum){ 
     return initialNum; 
     } 
     //recursive case 
     else{ 
     return initialNum + powerCount(initialNum *2, finalNum); 
     }//end of else 
    }//end of method5 
}//end of class 

ここでは、また、それを返す/呼び出し、あなたの最大の問題は、あまりにも多くのコードです

// method call to 5th method  
result2 = powerCount(1,(int)Math.pow(2,num)); 
// display output of 5th method to screen  
System.out.println(result2); 
+0

'(問題はpowerCountメソッドです).' - よく関連するコードを投稿してください。 –

+0

oops申し訳ありませんが、編集 – Siegfraud245

+0

'Math.pow(x、2)'は 'x * x'ではなく' x * 2'と同じです – 4castle

答えて

1

それを印刷し、私の主な方法であります私はそれをデバッグしようとはしません。あなたがものを印刷したい場合は、この方法でそれをしない

int pow(int x, int p) { 
    return p == 0 ? 1 : x * pow(x, p - 1); 
} 

再帰IMPLは同じくらい簡単です。私は、このコードの外側で、pを1からnまで繰り返すメインループで実行します(「効率」について忘れてください - これは数マイクロ秒で実行されます)。

0

使用可能な機能があったであろう:1でEXPを軽減しかし、多分あなたはより良い削減を見つけることができます。もちろん、

/** 
* Calculates n<sup>exp</sup>. 
* @param n the base number. 
* @param exp the exponent. 
* @return the power of n to the exp. 
*/ 
public static int power(int n, int exp) { 
    // What is the easiest case (terminating the recursion? 
    // - Case exp == 0 return 1 
    ... 
    // - Case other exp values 
    // What do you know about power formulas to reduce exp? 
    ... recursion with smaller exp 
} 

n^exp+1 == n * n^exp

あなたの場合、ブロックは数学的ではない長い名前であることがあります。これは時には曖昧なものです。名前としてx^nを使用すると、さらに役立つかもしれません。

また、再帰はf(x, y)で始まり、次のステップでは一種の削減であるf(x', y')を使用します。したがって、最後のステップではy'は簡単なケースで、x'の結果が出ている可能性があります。

power(x, n)は最後の再帰呼び出しpower(x_up_n, 1)またはx_up_n * power(rumpelstielchen, 0)を持ちます。

関連する問題