Last active
July 28, 2019 22:48
-
-
Save FRex/ef2dc53883d02bcd73e3d0a5301ac2f3 to your computer and use it in GitHub Desktop.
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 <vector> | |
#include <iostream> | |
#include <cstdio> | |
static unsigned minelem(const std::vector<unsigned>& a) | |
{ | |
unsigned ret = static_cast<unsigned>(-1); | |
for(unsigned x : a) | |
if(x && x < ret) | |
ret = x; | |
return ret; | |
} | |
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; | |
} | |
static void pvec(const char * a, const std::vector<unsigned>& b, int num) | |
{ | |
printf("%3d: ", num); | |
std::cout << a; | |
for(unsigned x : b) | |
std::printf("%3u, ", x); | |
std::cout << std::endl; | |
} | |
static int countNonZero(const std::vector<unsigned>& a) | |
{ | |
int ret = 0; | |
for(unsigned x : a) | |
ret += !!x; | |
return ret; | |
} | |
static unsigned occurs(const std::vector<unsigned>& a, unsigned e) | |
{ | |
unsigned ret = 0u; | |
for(unsigned x : a) | |
if(e == x) | |
++ret; | |
return ret; | |
} | |
static std::vector<unsigned> dec1(std::vector<unsigned> a) | |
{ | |
for(unsigned& x : a) | |
if(x) | |
--x; | |
return a; | |
} | |
static void steps(std::vector<unsigned> a, int& d1c, int& m0c) | |
{ | |
d1c = 0; | |
while(true) | |
{ | |
const unsigned m = minelem(a); | |
const unsigned o = occurs(a, m); | |
if(m > o) | |
break; | |
a = dec1(a); | |
++d1c; | |
}//while true | |
m0c = countNonZero(a); | |
} | |
static std::vector<unsigned> mak0(std::vector<unsigned> a) | |
{ | |
for(unsigned& x : a) | |
{ | |
if(x) | |
{ | |
x = 0; | |
break; | |
} | |
} | |
return a; | |
} | |
int main() | |
{ | |
std::vector<unsigned> a; | |
unsigned in; | |
while(std::cin >> in) | |
a.push_back(in); | |
int d1c, m0c; | |
steps(a, d1c, m0c); | |
pvec("For : ", a); | |
std::cout << "Answer is: " << d1c + m0c << std::endl; | |
std::cout << "Steps are: " << std::endl; | |
for(int i = 0; i < d1c; ++i) | |
pvec("Dec 1 to: ", a = dec1(a), i); | |
for(int i = 0; i < m0c; ++i) | |
pvec("Make 0 to: ", a = mak0(a), d1c + i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment