这是程序的核心部分。
给定
a[i] 其中 i 的范围从 0 到 n-1
b[i] 其中 i 的范围从 0 到 n-1。
假设 n = 4 用于演示目的
代码从这里开始
int count,sum=1;
for(count = 0; count < n; count++)
{
b[count] = sum;
sum *= a[count];
}
/*
循环计数 = 0
b[0] = 1;
sum = a[0]
循环计数 = 1
b[1] = a[0]
sum = a[0] a[1]
循环计数 = 2
b[2] = a[0] a[1]
sum = a[0] a[1] a[2]
循环计数 = 3
b[3] = a[0] a[1] a[2]
sum = a[0] a[1] a[2] a[3] -- This sum is not used at all
在循环结束时
b[0] = 1;
b[1] = a[0]
b[2] = a[0] a[1]
b[3] = a[0] a[1] a[2]
*/
sum = 1;
for(count = n-1; count >= 0; count--)
{
b[count] *= sum;
sum *= a[count];
}
/*
在 LOOP 开始时
b[3] = a[0] a[1] a[2]
b[2] = a[0] a[1]
b[1] = a[0]
b[0] = 1;
循环计数 = 3
b[3] = a[0] a[1] a[2] -- multiplied with 1 will lead to the same answer
sum = a[3]
循环计数 = 2
b[2] = a[0] a[1] a[3]
sum = a[3] a[2]
循环计数 = 1
b[1] = a[0] a[2] a[3]
sum = a[3] a[2] a[1]
循环计数 = 0
b[0] = a[1] a[2] a[3] -- Multiplied with 1 will lead to the same answer
sum = a[3] a[2] a[1] a[0] -- This sum is not used at all.
在循环结束时
b[0] = a[1] a[2] a[3]
b[1] = a[0] a[2] a[3]
b[2] = a[0] a[1] a[3]
b[3] = a[0] a[1] a[2]
*/