了解求和算法 [英] Understanding a summation algorithm
问题描述
如何做下面的工作?
数字算法1总和在阵列
功能总和(顺序)
总之< --- 0
对于i< ---- 1,以长度(序列)办
点心< ---总和+序列[I]
结束了
回报总和
它基本上是说,如果你开始在一个数组(它的第一个元素)的开始,在每一个该元素的值添加到正在运行的总时到达最后一个元素(序列的长度),你将有数组的值的总和。
#这是说我们有一个叫做加法功能,它需要一个序列
功能总和(顺序)
#这是说的0值赋给变量总和
总之< --- 0
#这是说make变量'我'从1到序列的长度
对于i< ---- 1,以长度(序列)办
#这是说分配总和+第i个值序列中变量总和
点心< ---总和+序列[I]
#这表明我们正与循环完成的,因此环回顶端,或
#继续,如果我们做循环
结束了
#这只是返回值相加调用过程
回报总和
我们在这里是没有任何特定的语言。相反,它是伪code - 它的意思是看起来足够像code,以传达一种算法或其他code有关的想法,但有意意味着不能在特定的语言。通过这种方式,它更容易接近的人谁读code,而不需要他们有特定的语言知识。
How does the following work?
Algorithm 1 Sum of numbers in an Array
Function SUMMATION (Sequence)
sum <---0
for i <----1 to Length (Sequence) do
Sum <--- Sum + sequence[i]
end for
return sum
It's basically saying that if you start at the beginning of an array (its first element), and at every element add that element's value to a running total, when you reach the last element (the length of the sequence) you'll have the sum of the values of the array.
# This says "we have a function called SUMMATION. It requires a sequence
Function SUMMATION (Sequence)
# This says "assign the value 0 to variable 'sum'
sum <---0
# This says "make variable 'i' go from 1 to the length of the sequence"
for i <----1 to Length (Sequence) do
# This says "assign 'sum + the ith value in the sequence' to variable 'sum'
Sum <--- Sum + sequence[i]
# This indicates we're done with the loop, so loop back to the top, or
# continue on if we're done looping
end for
# This just returns the value 'sum' to the calling procedure
return sum
What we have here isn't in any particular language. Instead, it's "Pseudocode" - it's meant to look enough like code in order to convey an algorithm or other code-related idea, but is intentionally meant to not be in a particular language. In this manner, it's more readily accessible to anyone who reads code without requiring they have knowledge of a particular language.
这篇关于了解求和算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!