/* 题目: 链接:https://www.nowcoder.com/questionTerminal/94a4d381a68b47b7a8bed86f2975db46 来源:牛客网 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1], 其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。 思路: C[i] = A[0]*...*A[i-1]; D[i] = A[i+1]*...*A[n-1]; B[i] = C[i]*D[i]; **/ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespace std; vector<int> multiply(const vector<int>& A) { vector<int> B; int length = A.size(); B.push_back(1); for(int i = 1; i < length; i++){ B.push_back(B.back()*A[i-1]); } int temp = 1; for(int i = length-2; i >= 0; i--){ temp *= A[i+1]; B[i] *= temp; } return B; } int main(){ vector<int> A={1,2,3,4,5}; vector<int> B=multiply(A); for(int i = 0; i < B.size(); i++){ cout<<B[i]<<" "; } }