Skip to content

Instantly share code, notes, and snippets.

@jabailey
Created March 6, 2018 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabailey/549ee93092e2f9e13a7f7d4ad5af6340 to your computer and use it in GitHub Desktop.
Save jabailey/549ee93092e2f9e13a7f7d4ad5af6340 to your computer and use it in GitHub Desktop.
wtf.cpp
#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
vector<int> M = {2,4,7,8,14,16,28,56};
bool f1(int& j, int& k, int rows, int cols) {
if(++k >= cols) {
k = 0;
if(++j >= rows) {
return false;
}
}
return true;
}
bool f2(int& j, int& k, int rows, int cols) {
if(++j >= rows) {
j = 0;
if(++k >= cols) {
return false;
}
}
return true;
}
// ... more functions with signature bool(int&, int&, int, int)
vector< function<bool(int&,int&,int,int)> > F = {f1, f2 /*...*/};
vector<int> solve(int m, function<bool(int&,int&,int,int)>& f,
vector< vector<int> >& input)
{
int i = 0;
int j = 0;
int k = 0;
int l = 0;
vector<int> result = {0};
do {
result[i] += input[j][k] * pow(2, l);
if (++l > m) {
l = 0;
i++;
result.push_back(0);
}
} while( f(j, k, input.size(), input[0].size()));
return result;
}
int main(int argc, char** argv) {
cout << "Enter number of rows: " << endl;
int rows;
cin >> rows;
cout << "Enter number of cols: " << endl;
int cols;
cin >> cols;
vector< vector<int> > input;
for (int i=0; i<rows; ++i) {
input.push_back(vector<int>());
for (int j=0; j<cols; ++j) {
cout << "Enter value " << i << "," << j << ":" << endl;
int value;
cin >> value;
input[i].push_back(value);
}
}
for ( int m: M) {
for (auto f: F) {
vector<int> result = solve(m, f, input);
cout << "Result: ";
for ( auto res: result ) {
cout << res << ",";
}
cout << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment