I don't understand the sigma notation and for loop

后端 未结 7 564
忘掉有多难
忘掉有多难 2021-01-05 04:10

I have the book saying that

\"enter

and says that this is equivalent to saying

7条回答
  •  时光取名叫无心
    2021-01-05 04:41

    Looks like the book just says that for given x and y

    for (int i = x; i <= y; ++i) Sum += 1;
    

    is similar to

    and thus, replacing x = 1 and y = N,

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

    is similar to

    This is of course what you expect: when counting from 1 to N, you count a total of N items. More generally, when counting from x to y, you count y - x + 1 items (+ 1 since you include both boundaries).

提交回复
热议问题