반갑습니다!

[프로그래머스] 내적 본문

알고리즘 문제 풀이

[프로그래머스] 내적

김덜덜이 2020. 11. 7. 15:26

풀이

벡터의 내적을 구현해주면 된다. 문제에서 알려준대로 그대로 구현하면 쉽게 풀 수 있다.

코드

C++

#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;
}
view raw 내적.cpp hosted with ❤ by GitHub

Java

class Solution {
public int solution(int[] a, int[] b) {
int answer = 0;
for(int i=0; i<a.length; i++)
answer += (a[i] * b[i]);
return answer;
}
}
view raw 내적.java hosted with ❤ by GitHub

Python3

def solution(a, b):
answer = 0
for i in range(len(a)):
answer += (a[i] * b[i])
return answer
view raw 내적.py hosted with ❤ by GitHub

Kotlin

class Solution {
fun solution(a: IntArray, b: IntArray): Int {
var answer = 0
for (i in a.indices)
answer += (a[i] * b[i])
return answer
}
}
view raw 내적.kt hosted with ❤ by GitHub

'알고리즘 문제 풀이' 카테고리의 다른 글

[백준] 1956 운동  (0) 2020.11.26
[백준] 2075 N번째 큰 수  (0) 2020.11.08
[백준] 2959 거북이  (0) 2020.11.04
[백준] 12096  (0) 2020.11.04
[백준] 15663 N과 M (9)  (0) 2020.11.02