Two n-dimensional vectors. The point multiplication of a vector is to add the product of the corresponding dimension of the vector. However, we can get a smaller vector point multiplication under the vector dimension switch, for example, a three-dimensional vector: [1, 3 ,? 5 and 4 ,? 2 ,? 1], the smallest vector point multiplication is-27, the dimension is changed to: [3, 1,-5] and [-2,-], the program design requirements: enter an integer n as the vector dimension, then enter two n-dimensional vectors, use spaces to distinguish vector elements, and output a line containing an integer, which is the smallest vertex multiplication.
The code I wrote is:
# Include
# Include
Using namespace std; int fun (int *, int *, const int); void main () {cout <"please enter the dimension of the vetor:"; int n; cin> n; cout <"the first vector:"; int * x = new int [n]; for (int I = 0; I
> X [I];} cout <"the second vector:"; int * y = new int [n]; for (int I = 0; I
> Y [I];} cout <
Vec; for (int I = 0; I
The biggest problem encountered when writing a program is how to arrange an array in full. I did not write the code successfully. Then I checked whether such a function exists, in c ++, we found that the functions that can be fully arranged by containers or arrays are sort () and next_permutation (), which solves the problem of fully arranged traversal, in the program, I arrange the index of a vector in full, and calculate the dot product size and return the minimum value.
Of course, you can also directly arrange all the elements of a vector, and then calculate the corresponding dot product, and return the minimum value:
# Include
# Include
Using namespace std; int fun (int *, int *, const int); inline void res (int * H, int n); inline int fac (int x ); void main () {cout <"please enter the dimension of the vetor:"; int n; cin> n; cout <"the first vector :"; int * x = new int [n]; for (int I = 0; I
> X [I];} cout <"the second vector:"; int * y = new int [n]; for (int I = 0; I
> Y [I];} cout <
Vec; while (next_permutation (A, A + n) {for (int I = 0; I
: Iterator ite = vec. begin (); ite
The output section of the Code above adds the arrangement of the vector elements corresponding to the final smallest dot product.