Created
July 28, 2019 18:23
-
-
Save FRex/61d692eb2cc3c2e05bf593ac8f14f251 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <vector> | |
#include <iostream> | |
#include <cstdio> | |
static bool isall0(const std::vector<unsigned>& a) | |
{ | |
for(unsigned x : a) | |
if(x) | |
return false; | |
return true; | |
} | |
static std::vector<unsigned> dec1(std::vector<unsigned> a) | |
{ | |
for(unsigned& x : a) | |
if(x) | |
--x; | |
return a; | |
} | |
static std::vector<unsigned> mak0(std::vector<unsigned> a) | |
{ | |
for(unsigned& x : a) | |
{ | |
if(x) | |
{ | |
x = 0; | |
break; | |
} | |
} | |
return a; | |
} | |
static void pvec(const char * a, const std::vector<unsigned>& b) | |
{ | |
std::cout << a; | |
for(unsigned x : b) | |
std::printf("%3u, ", x); | |
std::cout << std::endl; | |
} | |
enum action {action_d1, action_m0}; | |
static unsigned steps(const std::vector<unsigned>& a, std::vector<action> * whatdo) | |
{ | |
if(isall0(a)) | |
return 0; | |
const unsigned x = steps(dec1(a), 0x0); | |
const unsigned y = steps(mak0(a), 0x0); | |
const unsigned ret = 1 + std::min<unsigned>(x, y); | |
if(!whatdo) | |
return ret; | |
if(x <= y) | |
{ | |
whatdo->push_back(action_d1); | |
steps(dec1(a), whatdo); | |
} | |
else | |
{ | |
whatdo->push_back(action_m0); | |
steps(mak0(a), whatdo); | |
} | |
return ret; | |
} | |
int main() | |
{ | |
std::vector<unsigned> a; | |
std::vector<action> whatdo; | |
unsigned in; | |
while(std::cin >> in) | |
a.push_back(in); | |
const unsigned z = steps(a, &whatdo); | |
pvec("For : ", a); | |
std::cout << "Answer is: " << z << std::endl; | |
std::cout << "Steps are: " << std::endl; | |
for(int i = 0; i < (int)whatdo.size(); ++i) | |
{ | |
std::printf("%3d: ", i); | |
switch(whatdo[i]) | |
{ | |
case action_d1: | |
a = dec1(a); | |
pvec("Dec 1 to: ", a); | |
break; | |
case action_m0: | |
a = mak0(a); | |
pvec("Make 0 to: ", a); | |
break; | |
}//switch | |
}//for | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment