내적 함수 inner_product()

Subin·2024년 9월 10일

Algorithm

목록 보기
31/69

내적하는 문제를 풀다가
나는 이렇게 하나씩 돌면서 곱했는데

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> a, vector<int> b) {
    int answer = 0;

    for(int i=0; i<a.size(); i++)
    {
        answer += a[i] * b[i];
    }

    return answer;
}

이렇게 내적함수를 활용하는 방법이 있었다.
inner_product(a.begin(),a.end(),b.begin(),0);

#include <vector>
#include <numeric>
using namespace std;

int solution(vector<int> a, vector<int> b) {    
    return inner_product(a.begin(),a.end(),b.begin(),0);
}

#include <numeric> 을 사용해야 한다.

profile
성장하며 꿈꾸는 삶을 살아가고 있는 대학생입니다😊

0개의 댓글