
일단 가장 단순한 접근으로 for문 돌려서 풀어봄
#include <string> #include <vector> using namespace std; int solution(vector<int> absolutes, vector<bool> signs) { int answer = 0; for(int i=0; i < absolutes.size(); i++){ if(signs[i] == true) answer += absolutes[i]; else answer -= absolutes[i]; } return answer; }
웅. 넵. 그리고 더 획기적인 방법을 찾으려고 했는데 실패함
다른 사람들의 풀이를 볼게욥

획기적인 풀이가 없네.. 그럼 한문제 더 풀어야겠어요

오마이갓 터무니없이 쉬움..
#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; }
다른 사람의 풀이를 볼게용

우와 <numeric>에 내적해주는 함수도 구현되어있겠넹.
std::inner_product - cppreference.com
#include <numeric> 해주고, a의 첫번째 element와 끝 element, b의 첫번째 element, 그리고 초기값을 매개변수로 전해주면 된다.