Created
March 6, 2018 23:54
-
-
Save jabailey/549ee93092e2f9e13a7f7d4ad5af6340 to your computer and use it in GitHub Desktop.
wtf.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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