我不明白sigma表示法和for循环 [英] I don't understand the sigma notation and for loop

查看:131
本文介绍了我不明白sigma表示法和for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本书说





,并说这等同于

  for(i = 1; i< = N; i ++)
Sum + = i;

它进一步说,利用这个简单的公式,





最大值-最小值+ 1)



,并说将其更改为C代码将是

  for(k = j; k< = 1; k ++)
Sum + = k;

但是,我真的无法理解!有人可以向我解释吗?????



非常感谢

解决方案

关于第二个总和,起始于 k = j ,终止于 i 。该总和不等于(j-i-1),而是等于(i-j + 1)。让我们举个例子:



如果 j = 3 i = 6 ,则 k = 3 sum = 1 + 1 + 1 + 1 = 4 。通过应用公式: sum =(i-j + 1)= 6-3 +1 = 4



现在,第一个示例的C代码说:


for(i = 1; i <= N; i ++)
Sum + = i;


这是错误的。相反,它应该是:

  for(i = 1; i< = N; i ++)
Sum + = 1;

在第二个中,他们说:



< blockquote>

for(k = j; k< = 1; k ++)
Sum + = k;


它应该是:

  for(k = j; k< = i; k ++)
总和== 1; //其中i> = j


I have the book saying that

and says that this is equivalent to saying

for(i = 1; i <= N; i++)
    Sum += i;

and it further says utilising this simple formula,

since (maximum value - minimum value + 1 )

and it says changing this to C code would be

for(k = j; k <= 1; k++)
    Sum += k;

However, I seriously cannot understand this! can anyone explain this to me?????

Thank you very much

解决方案

About the second sum, it starts at k=j and ends at i. That sum is not equal to (j - i - 1), instead, it's equal to (i - j + 1). Let's do an example :

If j = 3 and i = 6, then k = 3 and sum = 1+1+1+1 = 4. By applying the formula : sum = (i - j + 1) = 6 - 3 + 1 = 4.

Now, the C code for the first example, they said :

for(i = 1; i <= N; i++) Sum += i;

This is wrong. Instead, it should be :

for(i = 1; i <= N; i++)
    Sum += 1;

In the second one, they said :

for(k = j; k <= 1; k++) Sum += k;

Instead, it should be :

for(k = j; k <= i; k++)
    Sum += 1;     // Where i >= j

这篇关于我不明白sigma表示法和for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆