Vector point multiplication minimum

Source: Internet
Author: User

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.
               



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.