Post

[월간 코드 챌린지 시즌1] 내적 (C++)

https://school.programmers.co.kr/learn/courses/30/lessons/70128

새로 푼 코드 (2023.04.25)

1
2
3
4
5
6
7
8
9
10
11
#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;
}

이전 코드 (2022.10)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> a, vector<int> b) {
    int answer = 0;
    int length = a.size();
    
    for(int i = 0; i < (length / 2); i++){
        answer += (a[i] * b[i]);
        answer += (a[length - i - 1] * b[length - i - 1]);
    }
    
    if(length % 2 != 0){
        int mid = (length / 2);
        answer += (a[mid] * b[mid]);
    }
    
    return answer;
}
This post is licensed under CC BY 4.0 by the author.