找到一个数的幂给出无关的答案。有人能告诉我原因吗?

huangapple go评论84阅读模式
英文:

Finding power of a number giving irrelevant answer. Can anyone give me the reason?

问题

使用Java计算一个数的幂。

如果输入为5和9,它会产生无关的答案。

import java.util.*;
import java.io.*;

class Solution {

    public static void main(String args[]) {

        // 在这里编写代码
        Scanner sc = new Scanner(System.in);

        int x = sc.nextInt();
        int n = sc.nextInt();

        System.out.println((n == 0) ? 1 : findPower(x, n));
    }

    static int findPower(int x, int n) {
        for (int i = 1; i <= n; i++) {
            x *= x;
        }
        return x;
    }
}

输出的截图

英文:

Finding the power of a number using java.

if the input takes 5 and 9 as input it produces irrelevant answer.

import java.util.* ;
import java.io.*; 
class Solution {
	
	public static void main(String args[]) {
		
		// Write code here
		Scanner sc = new Scanner(System.in);

		int x = sc.nextInt();
		int n = sc.nextInt();

		System.out.println((n==0)?1:findPower(x,n));	
	}
	static int findPower(int x,int n){
		for(int i=1;i&lt;=n;i++){
			x*=x;
		}
		return x;
	}
}

Screenshot of the output

答案1

得分: 1

你是将输入的 n 次方,而不是将输入乘以 n 次。你可以在循环中从 1 开始计算结果,然后不断将其乘以 x

int res = 1;
for (int i = 0; i < n; i++) res *= x;
return res;
英文:

You are squaring the input n times instead of multiplying the input n times. You can start the result at 1, then keep multiplying it by x in the loop.

int res = 1;
for (int i = 0; i &lt; n; i++) res *= x;
return res;

答案2

得分: 0

> 找到一个给出无关答案的幂次问题。有人能告诉我原因吗?

通过添加一个打印语句,您可以看到发生了什么。

 int n = 9, x = 5;
 for(int i=1;i<=n;i++){
     System.out.printf("%12d = %12d * %12d%n", x*x, x, x);
     x = x * x;
 }

输出(因为您要求原因,整数溢出导致负值)。

          25 =            5 *            5
         625 =           25 *           25
      390625 =          625 *          625
 -2030932031 =       390625 *       390625
 -2052264063 =  -2030932031 *  -2030932031
 -1083564287 =  -2052264063 *  -2052264063
   781532673 =  -1083564287 *  -1083564287
 -1741784063 =    781532673 *    781532673
   -59967487 =  -1741784063 *  -1741784063

您每次都将数字与自身相乘。只需将int result值初始化为1,然后使用result = result * x;进行乘法。

您还可以使用流。

int n = 9, x = 5;
int result = IntStream.range(0,n).reduce(1, (a,b)->a*x);
System.out.println(result);

输出

1953125
英文:

> Finding power of a number giving irrelevant answer. Can anyone give me the reason?

By putting in a print statement you can see what is happening.

 int n = 9, x = 5;
 for(int i=1;i&lt;=n;i++){
     System.out.printf(&quot;%12d = %12d * %12d%n&quot;, x*x, x, x);
     x = x * x;
 }

prints (Since you asked for a reason, the ints are overflowing, resulting in negative values).

          25 =            5 *            5
         625 =           25 *           25
      390625 =          625 *          625
 -2030932031 =       390625 *       390625
 -2052264063 =  -2030932031 *  -2030932031
 -1083564287 =  -2052264063 *  -2052264063
   781532673 =  -1083564287 *  -1083564287
 -1741784063 =    781532673 *    781532673
   -59967487 =  -1741784063 *  -1741784063

You are multiplying the number by itself each time. Just initialize an int result value to 1 and multiply it using result = result * x;

You can also use streams.

int n = 9, x = 5;
int result = IntStream.range(0,n).reduce(1, (a,b)-&gt;a*x);
System.out.println(result);

prints

1953125


</details>



huangapple
  • 本文由 发表于 2023年7月17日 23:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定