これがプログラムのコア部分です。
与えられた
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]
count = 1 でループ
b[1] = a[0]
sum = a[0] a[1]
count = 2 でループ
b[2] = a[0] a[1]
sum = a[0] a[1] a[2]
count = 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
LOOPの最後に
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;
count = 3 でループ
b[3] = a[0] a[1] a[2] -- multiplied with 1 will lead to the same answer
sum = a[3]
count = 2 でループ
b[2] = a[0] a[1] a[3]
sum = a[3] a[2]
count = 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.
LOOPの最後に
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]
*/