Skip to content

Instantly share code, notes, and snippets.

@hare1039
Created March 12, 2021 18:31
Show Gist options
  • Save hare1039/be6f7195d184727f0ec3e359a141faea to your computer and use it in GitHub Desktop.
Save hare1039/be6f7195d184727f0ec3e359a141faea to your computer and use it in GitHub Desktop.
simple solution of sumof
#include <iostream>
#include <vector>
bool sum_of(std::vector<int> const & list, int const target, int const maxdep)
{
if (maxdep < 0 or target < 0)
return false;
if (target == 0)
return true;
for (int const & value : list)
if (sum_of(list, target - value, maxdep - 1))
{
std::cout << value << " ";
return true;
}
return false;
}
int main()
{
std::vector<int> sums;
for (int i = 2; i < 100; i++)
sums.push_back(i * i);
auto print_sum_of = [&sums](int target, int maxdep) {
std::cout << target << " = ";
sum_of(sums, target, maxdep);
std::cout << "\n";
};
for (int i = 2; i <= 5; i++)
{
std::cout << "by " << i << "\n";
print_sum_of(1035, i);
print_sum_of(1054, i);
print_sum_of(1078, i);
print_sum_of(2146, i);
print_sum_of(2356, i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment