2018年7月20日 星期五

Sum of the Product of all Pairs of Vector Elements

#include <vector>
using namespace std;

int calculate_pairs(vector<int> vec) {
    //----WRITE YOUR CODE BELOW THIS LINE----
    int result {0};
    for(int i=0; i<vec.size(); i++)
        for(int j = i + 1; j < vec.size(); j++)
            result += vec[i] * vec[j];
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
    return result;
}

沒有留言:

張貼留言