这是我的程序
// ************************************************************
// PowersOf2.java
//
// Print out as many powers of 2 as the user requests
//
// ************************************************************
import java.util.Scanner;
public class PowersOf2 {
public static void main(String[] args)
{
int numPowersOf2; //How many powers of 2 to compute
int nextPowerOf2 = 1; //Current power of 2
int exponent= 1;
double x;
//Exponent for current power of 2 -- this
//also serves as a counter for the loop Scanner
Scanner scan = new Scanner(System.in);
System.out.println("How many powers of 2 would you like printed?");
numPowersOf2 = scan.nextInt();
System.out.println ("There will be " + numPowersOf2 + " powers of 2 printed");
//initialize exponent -- the first thing printed is 2 to the what?
while( exponent <= numPowersOf2)
{
double x1 = Math.pow(2, exponent);
System.out.println("2^" + exponent + " = " + x1);
exponent++;
}
//print out current power of 2
//find next power of 2 -- how do you get this from the last one?
//increment exponent
}
}
问题是我不允许使用math.pow方法,我需要找到另一种方法来在while循环中获得正确的答案。
答案 0 :(得分:8)
2的权力可以简单地由Bit Shift Operators
计算int exponent = ...
int powerOf2 = 1 << exponent;
即使对于更一般的形式,您也应该不通过“乘以n
次”来计算指数。相反,你可以做Exponentiation by squaring
答案 1 :(得分:1)
您可以实现自己的power
功能。
power
函数的复杂性取决于您的要求和约束。
例如,您可以将指数约束为只有正整数。
以下是power
函数的示例:
public static double power(double base, int exponent) {
double ans = 1;
if (exponent != 0) {
int absExponent = exponent > 0 ? exponent : (-1) * exponent;
for (int i = 1; i <= absExponent; i++) {
ans *= base;
}
if (exponent < 0) {
// For negative exponent, must invert
ans = 1.0 / ans;
}
} else {
// exponent is 0
ans = 1;
}
return ans;
}
答案 2 :(得分:1)
您可以尝试根据以下explanation进行此操作:
public double myPow(double x, int n) {
if(n < 0) {
if(n == Integer.MIN_VALUE) {
n = (n+1)*(-1);
return 1.0/(myPow(x*x, n));
}
n = n*(-1);
return (double)1.0/myPow(x, n);
}
double y = 1;
while(n > 0) {
if(n%2 == 0) {
x = x*x;
}
else {
y = y*x;
x = x*x;
}
n = n/2;
}
return y;
}
答案 3 :(得分:0)
如果没有性能限制,您可以这样做:
double x1=1;
for(int i=1;i<=numPowersOf2;i++){
x1 =* 2
}
答案 4 :(得分:0)
这是一个允许负/正功率计算的帖子。
https://stackoverflow.com/a/23003962/3538289
处理具有O(log(n))复杂度的+/-指数的函数。
double power(double x, int n){
if(n==0)
return 1;
if(n<0){
x = 1.0/x;
n = -n;
}
double ret = power(x,n/2);
ret = ret * ret;
if(n%2!=0)
ret = ret * x;
return ret;
}
答案 5 :(得分:0)
目前还不清楚您对使用循环的评论是您的愿望还是要求。如果它只是一种欲望,那么你可以使用一种不依赖于Math.Pow
的数学身份。
xy = ey∙ln(x)
在Java中,这看起来像
public static double myPow(double x, double y){
return Math.exp(y*Math.log(x));
}
如果你真的需要一个循环,你可以使用类似下面的内容
public static double myPow(double b, int e) {
if (e < 0) {
b = 1 / b;
e = -e;
}
double pow = 1.0;
double intermediate = b;
boolean fin = false;
while (e != 0) {
if (e % 2 == 0) {
intermediate *= intermediate;
fin = true;
} else {
pow *= intermediate;
intermediate = b;
fin = false;
}
e >>= 1;
}
return pow * (fin ? intermediate : 1.0);
}
答案 6 :(得分:0)
// Set the variables
int numPowersOf2; //How many powers of 2 to compute
int nextPowerOf2 = 1; //Current power of 2
int exponent = 0;
/* User input here */
// Loop and print results
do
{
System.out.println ("2^" + exponent + " = " + nextPowerOf2);
nextPowerOf2 = nextPowerOf2*2;
exponent ++;
}
while (exponent < numPowersOf2);
答案 7 :(得分:0)
这是我如何管理而不使用&#34; myPow(x,n)&#34;,而是通过使用&#34;而#34;。 (我只学习了2周的Java,所以如果代码有点笨拙:)
String base ="";
String exp ="";
BufferedReader value = new BufferedReader (new InputStreamReader(System.in));
try {System.out.print("enter the base number: ");
base = value.readLine();
System.out.print("enter the exponent: ");
exp = value.readLine(); }
catch(IOException e){System.out.print("error");}
int x = Integer.valueOf(base);
int n = Integer.valueOf(exp);
int y=x;
int m=1;
while(m<n+1) {
System.out.println(x+"^"+m+"= "+y);
y=y*x;
m++;
}
答案 8 :(得分:0)
To implement pow function without using built-in Math.pow(), we can use the below recursive way to implement it. To optimize the runtime, we can store the result of power(a, b/2) and reuse it depending on the number of times is even or odd.
static float power(float a, int b)
{
float temp;
if( b == 0)
return 1;
temp = power(a, b/2);
// if even times
if (b%2 == 0)
return temp*temp;
else // if odd times
{
if(b > 0)
return a * temp * temp;
else // if negetive i.e. 3 ^ (-2)
return (temp * temp) / a;
}
}
答案 9 :(得分:0)
我知道这个答案很晚,但是如果允许您使用存储基数和指数的变量,则可以使用一个非常简单的解决方案。
public class trythis {
public static void main(String[] args) {
int b = 2;
int p = 5;
int r = 1;
for (int i = 1; i <= p; i++) {
r *= b;
}
System.out.println(r);
}
}
这将适用于正数和负数的基数,但不适用于负数的幂数。
答案 10 :(得分:0)
要在不使用Math.pow()
的情况下获取指数值,可以使用循环:
只要计数小于b(您的幂),您的循环就会有一个
附加的“ * a”。从数学上讲,它与拥有Math.pow()
while (count <=b){
a= a* a;
}