描述
Scenario
Several people are standing in a row divided into two teams.
The first person goes into team 1, the second goes into team 2, the third goes into team 1, and so on.
Task
Given an array of positive integers (the weights of the people), return a new array/tuple of two integers, where the first one is the total weight of team 1, and the second one is the total weight of team 2.
Notes
Array size is at least 1.
All numbers will be positive.
Input >> Output Examples
rowWeights([13, 27, 49]) ==> return (62, 27)
Explanation:
The first element 62 is the total weight of team 1, and the second element 27 is the total weight of team 2.
rowWeights([50, 60, 70, 80]) ==> return (120, 140)
Explanation:
The first element 120 is the total weight of team 1, and the second element 140 is the total weight of team 2.
rowWeights([80]) ==> return (80, 0)
Explanation:
The first element 80 is the total weight of team 1, and the second element 0 is the total weight of team 2.
测试用例:
#include <vector>
#include <fstream>
#include <random>
#include <cstdlib>
#include <ctime>
#include <utility>
using namespace std ;
using vec = vector <int> ;
using p = pair <int, int>;
Describe(Row_Weights)
{
It(Basic_Tests)
{
Assert::That(rowWeights(vec{80}), Equals(p{80,0}));
Assert::That(rowWeights(vec{100,50}),Equals(p{100,50}));
Assert::That(rowWeights(vec{50,60,70,80}),Equals(p{120,140}));
}
It(Odd_Vector_Length)
{
Assert::That(rowWeights(vec{13,27,49}),Equals(p{62,27}));
Assert::That(rowWeights(vec{70,58,75,34,91}),Equals(p{236,92}));
Assert::That(rowWeights(vec{29,83,67,53,19,28,96}),Equals(p{211,164}));
}
It(Even_Vector_Length)
{
Assert::That(rowWeights(vec{100,50}),Equals(p{100,50}));
Assert::That(rowWeights(vec{100,51,50,100}),Equals(p{150,151}));
Assert::That(rowWeights(vec{39,84,74,18,59,72,35,61}),Equals(p{207,235}));
}
pair<int, int> modelSolution(const vector<int>& weights)
{
pair<int, int> res{0, 0};
for (vec::size_type i = 0; i < weights.size(); i++)
(i % 2 == 0 ? res.first : res.second) += weights[i];
return res;
}
It(Test_Random_Numbers)
{
srand(time(0));
for(int i = 0; i < 100; i++)
{
vector<int> random_input;
int randomTestInputSize = 3 + rand() % 199; // Vector size range: [3, 202)
for(int i = 0; i < randomTestInputSize; i++)
{
random_input.push_back(1 + rand() % 200); // Element value range: [1, 200)
}
Assert::That(rowWeights(random_input) , Is().EqualTo(modelSolution(random_input )));
}
}
};
解决方案
常规做法
#include <vector>
#include <utility>
using namespace std;
pair<int,int> rowWeights (const vector <int> &weights)
{
int sum1{}, sum2{};
for (int i = 0; i < weights.size(); i++)
i % 2 ? sum2 += weights[i] : sum1 += weights[i];
std::pair <int, int> pr{ sum1, sum2 };
return pr;
}
#include <vector>
#include <utility>
using namespace std;
pair<int,int> rowWeights (const vector <int> &weights)
{
int team = 0, teamWeights[2] = { 0 };
for (auto w : weights) teamWeights[(team++) % 2] += w;
return make_pair(teamWeights[0], teamWeights[1]);
}
使用accumulate
#include <vector>
#include <utility>
#include <numeric>
using namespace std;
pair<int,int> rowWeights (const vector <int> &weights)
{
bool b = 0;
return std::accumulate(weights.begin(),weights.end(),std::make_pair<int,int>(0,0),
[&b](auto p, int w)->pair<int,int>{
b = !b;
return {p.first+b*w,p.second+(!b)*w};
});
}
使用for_each
#include <vector>
#include <utility>
#include <algorithm>
std::pair<int,int> rowWeights (const std::vector<int> &weights) {
std::pair<int, int> result;
std::for_each(weights.begin(), weights.end(), [&, count = 0](const int& weight) mutable {
(count++%2 == 0 ? result.first : result.second) += weight;
});
return result;
}