Confusion with Algorithm for n + (n-1) + (n-2)

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

Confusion with Algorithm for n + (n-1) + (n-2)

问题

我正在尝试在使用递归后扩展此答案的迭代,但对于某些变量,我得到了错误的答案。

问题是要编写一个代码,以计算 n + (n-1) + (n-2)... 直到达到 3 + 2 + 1。

我尝试了以下 Java 代码。

for (int i = 1; i < num; i++) {
  s += n - 1;
}
return s;

其中 n 是用户输入的变量,s 是某种求和变量。请问,s += n - 1 的含义是什么?我现在很难理解它的位置。谢谢!

此外,我还尝试了将条件改为 i <= num,并返回 s-1;但也没有奏效。

英文:

I am trying to expand this answer in iteration after using recursion but i am getting the wrong answer for certain variables.

the question was to write a code for n + (n-1) + (n-2)... till it gets to 3 + 2 + 1

i did it with recursion and it works great but the iteration is the one giving me problem.

this is what i tried in java.

for (int i =1; i &lt;num; i ++) {
  s += n -1;
  }
  return s;

where n is the user inputed varibale, and s is some kind of sum variable.
Also please can you explain what the s+=n -1 means?

i am finding it hard to place it right now. Please and Thank you!

Also i tried to put it <= num
and return s-1; but it did not work either

答案1

得分: 2

以下是翻译好的代码部分:

要做到与您描述的完全相同,您可以这样做。

int sum = 0;
int n = 100;
for (int i = 0; i < n; i++) {
   sum += n - i; // 首先是 n,然后是 n-1,然后是 n-2,...
}

但这与以下相同,只是顺序相反。

for (int i = 1; i <= n; i++) {
   sum += i;
}

如评论中所述,差值为1的算术级数总和,对于任何值n,都是

sum = (n*(n+1))/2;

上述所有内容都产生相同的结果。

英文:

To do exactly as you described, you can do it like this.

int sum = 0;
int n = 100;
for (int i = 0; i &lt; n; i++) {
   sum += n-i; // first n, then n-1, then n-2, ...
}

But that is the same as the following, only in reversed order.

for (int i = 1; i &lt;= n; i++) {
   sum += i;
}

and as stated in the comments, the arithmetic series sum where the difference between adjacent values is 1, for any value n, is

sum = (n*(n+1))/2;

All the above yield the same result.

答案2

得分: 0

看着你的代码,你甚至没有添加正确的内容到s中:

  • 你添加了n-1,但那甚至不包含你的迭代变量i,而且,你正在迭代到num,而不是n - 你应该添加num-i
  • 此外,检查一下你的s最初是什么,如果你在上面有int s = num;,那就没问题。但如果你有int s = 0;,你要么将其更改为num,要么从i = 0开始。
英文:

looking at your code, you are not even adding the right thing to the s

  • you add n-1 but that doesn't even contain your iteration variable i and also, you are iterating to num, not 'n' - you should be adding num-i
  • also, check what your s initially is, if you got int s = num; somewhere above, it's ok. But, if you got int s = 0;, you will have to either change it to num or start at i = 0 instead

答案3

得分: 0

让我们按照给定的方式来做:我们应该循环遍历 n, n - 1, n - 2, ..., 3, 2, 1,所以我们

  • n 开始循环:int i = n
  • 循环直到 1i &gt;= 1
  • 递减循环变量(我们将其称为 i):--i

我们准备好编写 for 循环的标头:

for (int i = n; i &gt;= 1; --i)

而代码可以是

int n = 100;

int s = 0;

for (int i = n; i &gt;= 1; --i)
    s = s + i;

如果我们想将它包装成一个方法:

static int MySum(int n) {
  int s = 0;

  for (int i = n; i &gt;= 1; --i)
    s = s + i;

  return s;
}
英文:

Let's do it exactly as it's stated: we should loop over n, n - 1, n - 2, ..., 3, 2, 1, so we

  • start looping from n: int i = n
  • loop up and including 1: i &gt;= 1
  • decrement loop valitable (let it be i) by 1: --i

We are ready to write for loop caption:

for (int i = n; i &gt;= 1; --i)

And the code can be

int n = 100;

int s = 0;

for (int i = n; i &gt;= 1; --i)
    s = sum + i; 

If we want to wrap it into method:

static int MySum(int n) {
  int s = 0;

  for (int i = n; i &gt;= 1; --i)
    s = sum + i; 

  return s;
}

huangapple
  • 本文由 发表于 2023年4月13日 21:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005817.html
匿名

发表评论

匿名网友

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

确定