---
title: "计算数组元素的乘积"
author: "standalone"
date: "2010-11-03T20:56:17Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-06-04T08:50:01Z"
id: 802129
url: "https://standalone.iteye.com/blog/802129"
---

Given an array a\[i], your task is to generate an array b\[i], b\[i] is the product of the numbers in the array except the i th number.

 

My solution:

 

DP problem?

 

Define two functions:

f(i)=a\[0]\*a\[1]\*a\[2]\*...\*a\[i-1]

g(i)=a\[i+1]\*a\[i+2]\*...\*a\[n-1]

So b\[i]=f(i)\*g(i)

 

Then 

f(0)=1

f(1)=a\[0]

f(i+1)=f(i)\*a\[i]

 

Following this rule we can calculate f\[0], f\[1],...f\[n-1].

 

Similarly, 

g(n-1)=1

g(n-2)=a\[n-1]

g(i-1)=g(i)\*a\[i];

 

Thus we get g\[n-1], g\[n-2]...g\[0] in reverse sequence.

 

At last, we can calculate b\[i]=f(i)\*g(i).