#include <iostream> #include <vector> // Write a program that outputs all possibilities to put + or - or nothing // between the numbers 1, 2, ..., 9 (in this order) such that the result is // always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100. enum class Operation { Nothing, Add, Substract }; // Produce the next vector of Operations // \returns "true" if a vector was produced, "false" otherwise bool next(std::vector<Operation>& operations) { for (Operation& o: operations) { switch (o) { case Operation::Nothing: o = Operation::Add; return true; case Operation::Add: o = Operation::Substract; return true; case Operation::Substract: o = Operation::Nothing; break; // loop to increment next } } return false; // if we reach here, the vector is not long enough } // next // Given a list of integers and operations, return the total int compute(std::vector<int> const& suite, std::vector<Operations> const& operations) { // 1. First pass, group by nothing std::vector<int> catenated{1, suite[0]}; for (size_t i = 0, max = operations.size(); i < max; ++i) { if (operations[i] != Operation::Nothing) { catenated.push_back(suite[i+1]); continue; } int& last = catenated.back(); last *= 10; last += suite[i+1]; } // 2. Second pass, evaluate int total = catenated[0]; catenated.pop(); for (Operation const& o: operations) { switch (o) { case Operation::Nothing: break; case Operation::Add: total += catenated[0]; catenated.pop(); break; case Operation::Substract: total -= catenated[0]; catenated.pop(); break; } } return total; } // compute // Display the list of "+- " and its result void display(std::vector<int> const& suite, std::vector<Operations> const& operations, int const target) { std::cout << suite[0]; for (size_t i = 0, max = operations.size(); i < max; ++i) { switch (operations[i]) { case Operation::Nothing: break; case Operation::Add: std::cout << " + "; break; case Operation::Substract: std::cout << " - "; break; } std::cout << suite[i + 1]; } std::cout << " = " << target << "\n"; } // display // Output the lists of "+- " which achieve the goal for the given vector and the // given target number. void compute(std::vector<int> const& suite, int const target) { if (suite.empty()) { return; } Operation const N = Operation::Nothing; std::vector<Operation> operations{suite.size() - 1, N}; do { if (compute(suite, operations) == target) { display(suite, operations, target); } } while (next(operations)); } int main() { std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9}; compute(vec, 100); return 0; }
Standard input is empty
prog.cpp:35:19: error: ‘Operations’ was not declared in this scope std::vector<Operations> const& operations) ^~~~~~~~~~ prog.cpp:35:29: error: template argument 1 is invalid std::vector<Operations> const& operations) ^ prog.cpp:35:29: error: template argument 2 is invalid prog.cpp: In function ‘int compute(const std::vector<int>&, const int&)’: prog.cpp:40:38: error: request for member ‘size’ in ‘operations’, which is of non-class type ‘const int’ for (size_t i = 0, max = operations.size(); i < max; ++i) { ^~~~ prog.cpp:41:19: error: invalid types ‘const int[size_t {aka long unsigned int}]’ for array subscript if (operations[i] != Operation::Nothing) { ^ prog.cpp:52:38: error: ‘class std::vector<int>’ has no member named ‘pop’ int total = catenated[0]; catenated.pop(); ^~~ prog.cpp:54:27: error: ‘begin’ was not declared in this scope for (Operation const& o: operations) { ^~~~~~~~~~ prog.cpp:54:27: note: suggested alternative: In file included from /usr/include/c++/6/string:51:0, from /usr/include/c++/6/bits/locale_classes.h:40, from /usr/include/c++/6/bits/ios_base.h:41, from /usr/include/c++/6/ios:42, from /usr/include/c++/6/ostream:38, from /usr/include/c++/6/iostream:39, from prog.cpp:1: /usr/include/c++/6/bits/range_access.h:105:37: note: ‘std::begin’ template<typename _Tp> const _Tp* begin(const valarray<_Tp>&); ^~~~~ prog.cpp:54:27: error: ‘end’ was not declared in this scope for (Operation const& o: operations) { ^~~~~~~~~~ prog.cpp:54:27: note: suggested alternative: In file included from /usr/include/c++/6/string:51:0, from /usr/include/c++/6/bits/locale_classes.h:40, from /usr/include/c++/6/bits/ios_base.h:41, from /usr/include/c++/6/ios:42, from /usr/include/c++/6/ostream:38, from /usr/include/c++/6/iostream:39, from prog.cpp:1: /usr/include/c++/6/bits/range_access.h:107:37: note: ‘std::end’ template<typename _Tp> const _Tp* end(const valarray<_Tp>&); ^~~ prog.cpp:59:37: error: ‘class std::vector<int>’ has no member named ‘pop’ total += catenated[0]; catenated.pop(); ^~~ prog.cpp:63:37: error: ‘class std::vector<int>’ has no member named ‘pop’ total -= catenated[0]; catenated.pop(); ^~~ prog.cpp: At global scope: prog.cpp:73:20: error: ‘Operations’ was not declared in this scope std::vector<Operations> const& operations, ^~~~~~~~~~ prog.cpp:73:30: error: template argument 1 is invalid std::vector<Operations> const& operations, ^ prog.cpp:73:30: error: template argument 2 is invalid prog.cpp: In function ‘void display(const std::vector<int>&, const int&, int)’: prog.cpp:77:38: error: request for member ‘size’ in ‘operations’, which is of non-class type ‘const int’ for (size_t i = 0, max = operations.size(); i < max; ++i) { ^~~~ prog.cpp:78:23: error: invalid types ‘const int[size_t {aka long unsigned int}]’ for array subscript switch (operations[i]) { ^ prog.cpp:79:19: error: could not convert ‘Nothing’ from ‘Operation’ to ‘<type error>’ case Operation::Nothing: break; ^~~~~~~ prog.cpp:81:19: error: could not convert ‘Add’ from ‘Operation’ to ‘<type error>’ case Operation::Add: std::cout << " + "; break; ^~~ prog.cpp:83:19: error: could not convert ‘Substract’ from ‘Operation’ to ‘<type error>’ case Operation::Substract: std::cout << " - "; break; ^~~~~~~~~ prog.cpp: In function ‘void compute(const std::vector<int>&, int)’: prog.cpp:101:32: error: no matching function for call to ‘compute(const std::vector<int>&, std::vector<Operation>&)’ if (compute(suite, operations) == target) { ^ prog.cpp:34:5: note: candidate: int compute(const std::vector<int>&, const int&) int compute(std::vector<int> const& suite, ^~~~~~~ prog.cpp:34:5: note: no known conversion for argument 2 from ‘std::vector<Operation>’ to ‘const int&’ prog.cpp:94:6: note: candidate: void compute(const std::vector<int>&, int) void compute(std::vector<int> const& suite, int const target) { ^~~~~~~ prog.cpp:94:6: note: no known conversion for argument 2 from ‘std::vector<Operation>’ to ‘int’ prog.cpp:102:37: error: invalid initialization of reference of type ‘const int&’ from expression of type ‘std::vector<Operation>’ display(suite, operations, target); ^ prog.cpp:72:6: note: in passing argument 2 of ‘void display(const std::vector<int>&, const int&, int)’ void display(std::vector<int> const& suite, ^~~~~~~ prog.cpp: In function ‘int main()’: prog.cpp:109:18: error: call of overloaded ‘compute(std::vector<int>&, int)’ is ambiguous compute(vec, 100); ^ prog.cpp:34:5: note: candidate: int compute(const std::vector<int>&, const int&) int compute(std::vector<int> const& suite, ^~~~~~~ prog.cpp:94:6: note: candidate: void compute(const std::vector<int>&, int) void compute(std::vector<int> const& suite, int const target) { ^~~~~~~
Standard output is empty