Vector dot product calculation

Source: Internet
Author: User

Total time limit:
1000ms
Memory Limit:
65536kB
Describe

In linear algebra and computational geometry, the vector dot product is a very important operation.

Given two n-dimensional vectors a= (a1,a2,..., an) and b= (B1,b2,..., bn), find dot product a· b=a1b1+a2b2+...+anbn.

Input
The first line is an integer n. 1 <= n <= 1000.
The second line contains n integers a1,a2,..., an.
The third line contains n integers b1,b2,..., bn.
Adjacent integers are separated by a single space. The absolute value of each integer is no more than 1000.
Output
An integer, which is the dot product result of two vectors.
Sample input
31 4 62) 1 5
Sample output
36

The code is actually:

1#include <cstdio>2 intn,a[1010],b[1010],ans;3 intMain () {4scanf"%d",&n);5      for(intI=1; i<=n;i++) scanf ("%d",&a[i]);6      for(intI=1; i<=n;i++) scanf ("%d",&b[i]);7      for(intI=1; i<=n;i++) ans+=a[i]*B[i];8printf"%d\n", ans);9     return 0;Ten}

。。。

Vector dot product calculation

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.