为什么这会得到 n^n 的值,而我只是在计算 n^2?

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

Why does this give value n^n if I'm just doing n^2?

问题

我在Hackerrank上做一个问题,但是测试用例不对。在这里插入了一些System.out.println命令后,我终于找出了代码的问题所在:

问题出在了while循环中。我从while循环中去除了复杂性,在我的本地eclipse环境中执行了一个基本的循环,结果是相同的:

这是代码:

	int n=3;
		
	while(n<100)
	{
		System.out.println(n+" n WHILE");
		n=n*n;
		System.out.println(n*n+" n n WHILE");
	}

我原以为n会初始化为n*n,实际上它被初始化为n^n。
输出如下:

3 n WHILE
81 n n WHILE
9 n WHILE
6561 n n WHILE
81 n WHILE
43046721 n n WHILE

好的,我以为可能是一些初始化问题之类的,我修改了代码如下,但它仍然打印相同的输出。

while(n<100)
{
	System.out.println(n+" n WHILE");
	n=(int) Math.pow(n, 2);
	System.out.println(n*n+" n n WHILE");
}

上面的代码输出也是相同的。现在,我知道我可以添加一个重复的变量来正确初始化,但我的问题是为什么上述实现会失败?此外,除了使用重复的变量,我还有其他解决方法吗?什么是正确的做法?

附注:如果你想知道Hackerrank问题的链接和我的解决方案,我只会给出一个pastebin的链接,以免让这个已经很长的问题变得更长。
链接 = 链接

英文:

I am doing a problem in Hackerrank and I got the testcases wrong. After inserting System.out.println commands here and there I finally found out whats wrong with the code:

It was in the while loop. I removed complexities from the while loop and executed a basic loop in my local eclipse environment and the result was same:

This is the code:

	int n=3;
		
		while(n&lt;100)
		{
			System.out.println(n+&quot; n WHILE&quot;);
				n=n*n;
			System.out.println(n*n+&quot; n n WHILE&quot;);
		}


My understanding was that that n would initialize to n*n, instead it's being initialized to n^n.
The output is as follows:

3 n WHILE
81 n n WHILE
9 n WHILE
6561 n n WHILE
81 n WHILE
43046721 n n WHILE

Ok, I thought maybe some initialization problem or something and I modified the code as follows and its still the printing the same output.

	while(n&lt;100)
		{
			System.out.println(n+&quot; n WHILE&quot;);
			n=(int) Math.pow(n, 2);
			System.out.println(n*n+&quot; n n WHILE&quot;);
		}

The above code has same output too. Now, I know I can add a duplicate variable to initialize properly but my question is why do the above implementations fail? Also, is keeping a duplicate variable my only workaround? What's the proper way to do that?

PS: Incase you're wondering the hackerrank problem link and my solution, I'll just give a pastebin link instead to lengthening this already large question.
Link = Link

答案1

得分: 1

问题代码中没有在任何地方计算 n ^ n,但是你打印的方式使输出非常混乱,因为你在更新 n 之后才打印 n * n,所以实际上你在打印的是 n ^ 4

以下是更好版本的 print 语句:

int n = 3;
while (n < 100) {
    System.out.println("n = " + n + "  ->  " + n + " * " + n + " = " + n * n);
    n = n * n;
}
System.out.println("n = " + n);

输出

n = 3  ->  3 * 3 = 9
n = 9  ->  9 * 9 = 81
n = 81  ->  81 * 81 = 6561
n = 6561

你正在计算的是 n ^ (2 ^ i),其中 i 是循环的迭代次数,即:

  • 经过 1 次迭代:n = n * n = n^2 (3^2 = 9)
  • 经过 2 次迭代:n = (n * n) * (n * n) = n^4 (3^4 = 81)
  • 经过 3 次迭代:n = ((n * n) * (n * n)) * ((n * n) * (n * n)) = n^8 (3^8 = 6561)
  • 经过 4 次迭代:n = n^16 (3^16 = 43,046,721)
  • 经过 5 次迭代:n = n^32 (3^32 = 1,853,020,188,851,841)
  • 经过 6 次迭代:n = n^64 (3^64 = 3,433,683,820,292,512,484,657,849,089,281)
  • ...

这与 n ^ (i + 1) 相去甚远,后者可能是你的预期?

  • 经过 1 次迭代:n = n * n = n^2 (3^2 = 9)
  • 经过 2 次迭代:n = n * n * n = n^3 (3^3 = 27)
  • 经过 3 次迭代:n = n * n * n * n = n^4 (3^4 = 81)
  • 经过 4 次迭代:n = n * n * n * n * n = n^5 (3^5 = 243)
  • 经过 5 次迭代:n = n * n * n * n * n * n = n^6 (3^6 = 729)
  • 经过 6 次迭代:n = n * n * n * n * n * n * n = n^7 (3^7 = 2187)
  • ...

要实现这一点,你需要一个单独的结果变量:

int n = 3;
int r = n;
while (r < 100) {
    System.out.println("r = " + r + "  ->  " + r + " * " + n + " = " + r * n);
    r = r * n;
}
System.out.println("r = " + r);

输出

r = 3  ->  3 * 3 = 9
r = 9  ->  9 * 3 = 27
r = 27  ->  27 * 3 = 81
r = 81  ->  81 * 3 = 243
r = 243
英文:

The question code does not calculate n ^ n anywhere, but the way you print makes the output very confusing, because you're print n*n after already updating n, so you're actually printing n ^ 4.

Here is a better version of the print statements:

int n = 3;
while (n &lt; 100) {
	System.out.println(&quot;n = &quot; + n + &quot;  -&gt;  &quot; + n + &quot; * &quot; + n + &quot; = &quot; + n * n);
	n = n * n;
}
System.out.println(&quot;n = &quot; + n);

Output

n = 3  -&gt;  3 * 3 = 9
n = 9  -&gt;  9 * 9 = 81
n = 81  -&gt;  81 * 81 = 6561
n = 6561

What you are calculating is n ^ (2 ^ i) where i is the number of iterations of the loop, i.e.

  • After 1 iteration: n = n * n = n<sup>2</sup> &nbsp; (3<sup>2</sup> = 9)
  • After 2 iterations: n = (n * n) * (n * n) = n<sup>4</sup> &nbsp; (3<sup>4</sup> = 81)
  • After 3 iterations: n = ((n * n) * (n * n)) * ((n * n) * (n * n)) = n<sup>8</sup> &nbsp; (3<sup>8</sup> = 6561)
  • After 4 iterations: n = n<sup>16</sup> &nbsp; (3<sup>16</sup> = 43,046,721)
  • After 5 iterations: n = n<sup>32</sup> &nbsp; (3<sup>32</sup> = 1,853,020,188,851,841)
  • After 6 iterations: n = n<sup>64</sup> &nbsp; (3<sup>64</sup> = 3,433,683,820,292,512,484,657,849,089,281)
  • ...

That is a far cry from n ^ (i + 1), which might have been what you expected?

  • After 1 iteration: n = n * n = n<sup>2</sup> &nbsp; (3<sup>2</sup> = 9)
  • After 2 iterations: n = n * n * n = n<sup>3</sup> &nbsp; (3<sup>3</sup> = 27)
  • After 3 iterations: n = n * n * n * n = n<sup>4</sup> &nbsp; (3<sup>4</sup> = 81)
  • After 4 iterations: n = n * n * n * n * n = n<sup>5</sup> &nbsp; (3<sup>5</sup> = 243)
  • After 5 iterations: n = n * n * n * n * n * n = n<sup>6</sup> &nbsp; (3<sup>6</sup> = 729)
  • After 6 iterations: n = n * n * n * n * n * n * n = n<sup>7</sup> &nbsp; (3<sup>7</sup> = 2187)
  • ...

To do that, you need a separate result variable:

int n = 3;
int r = n;
while (r &lt; 100) {
	System.out.println(&quot;r = &quot; + r + &quot;  -&gt;  &quot; + r + &quot; * &quot; + n + &quot; = &quot; + r * n);
	r = r * n;
}
System.out.println(&quot;r = &quot; + r);

Output

r = 3  -&gt;  3 * 3 = 9
r = 9  -&gt;  9 * 3 = 27
r = 27  -&gt;  27 * 3 = 81
r = 81  -&gt;  81 * 3 = 243
r = 243

答案2

得分: 0

The line System.out.println(n*n+" n n WHILE"); prints the square of the current value of n.

If you remove this line, the output will make an awful lot more sense.

You are currently printing n, squaring n, and then printing the square of the new n (without squaring n a second time).

The lines ending in "n WHILE" start at 3 and keep squaring. They show the value of n.

The lines ending in "n n WHILE" start at 9 (3 squared) and keep squaring. They show the square of the value of n.

英文:

The line System.out.println(n*n+&quot; n n WHILE&quot;); prints the square of the current value of n.

If you remove this line, the output will make an awful lot more sense.

You are currently printing n, squaring n, and then printing the square of the new n (without squaring n a second time).

The lines ending in "n WHILE" start at 3 and keep squaring. They show the value of n.

The lines ending in "n n WHILE" start at 9 (3 squared) and keep squaring. They show the square of the value of n.

答案3

得分: 0

  1. 你将 n (n1) 设为 3。然后打印它。
  2. 接下来,你将 n (n2) 设为 n*n (n1*n1),然后在第二个打印语句中打印 n*n (n2*n2)。使用 n1、n2 的概念,你打印了 n2*n2 = (n1*n1)*(n1*n1) = n1^4,其值为 81

下一个循环打印 9^2 和 9^4。

英文:
  1. You are setting n (n1) to 3. Then print it.
  2. Next, you are setting n (n2) to n*n (n1*n1) and then print n*n (n2*n2) in the second print statement. Using the notion n1,n2 you print n2*n2 = (n1*n1)*(n1*n1) = n1^4 which is 81.

Next loop prints 9^2 and 9^4.

答案4

得分: 0

代码部分不要翻译,只返回翻译好的内容:

你打印新值的方式毫无意义。你将 n 初始化为 n^2,然后在你的打印语句中对其进行另一个 n^2 运算,其中的任何一个都可以工作。

while (n < 100) {
    System.out.println(n + " n WHILE");
    n = n * n;
    System.out.println(n + " n n WHILE");
}

while (n < 100) {
    System.out.println(n + " n WHILE");
    System.out.println(n * n + " n n WHILE");
    n = n * n;
}
英文:

The way you were printing the new value made no sense. You initialized n to n^2 and than did another n^2 on that in your print either of these would work.

 while(n&lt;100)
 {
       System.out.println(n+&quot; n WHILE&quot;);
       n=n*n;
       System.out.println(n+&quot; n n WHILE&quot;);
 }

 while(n&lt;100)
 {
      System.out.println(n+&quot; n WHILE&quot;);
      System.out.println(n*n+&quot; n n WHILE&quot;);
      n=n*n;         
 }

huangapple
  • 本文由 发表于 2020年9月11日 02:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63835613.html
匿名

发表评论

匿名网友

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

确定