cxx17/cc197.cc

26 lines
556 B
C++
Raw Permalink Normal View History

2017-07-09 21:53:30 +02:00
// Return the score for a mastermind guess.
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <range/v3/all.hpp>
using namespace ranges;
template<typename T>
static T max_seq(std::initializer_list<T> seq) {
2017-07-09 21:59:00 +02:00
T top = *seq.begin();
2017-07-09 21:53:30 +02:00
2017-07-09 21:59:00 +02:00
for (auto start : view::ints(0ul, seq.size())) {
for (auto stop : view::ints(start + 1, seq.size())) {
auto sum = accumulate(seq | view::slice(start, stop), 0);
2017-07-09 21:53:30 +02:00
top = std::max(sum, top);
}
}
return top;
}
TEST_CASE("cc19.9", "max_seq") {
CHECK(max_seq({2, -8, 3, -2, 4, -10}) == 5);
}