这是我最近参加采访时提出的一个问题,我觉得很有趣。假设 int n=10;
输入:一个数组 int a[10];
输出:一个数组float b[10];
要求:
b[0]= a[1]*a[2]*...a[9]; // product of all numbers in a, other than a[0];
b[1]= a[0]*a[2]*...a[9]; // product of all numbers in a, other than a[1];
....
b[9]= a[0]*a[1]*...a[8]; // product of all numbers in a, other than a[9];
....
问题:我们如何在不使用除法运算符b
的情况下填充数组?并使用算法?/
O(n)
我尝试了很多方法,但仍然是徒劳的。有任何想法吗?