1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <vector> #include <iostream>
using std::cout; using std::endl; using std::vector;
class CDigital {
public: CDigital(const vector<int> &bases) : bases(bases), curDig(0), digCount(0) {}
bool next() { if (this->curDig.empty() == true) { this->curDig.resize(bases.size(), 0);
++this->digCount; return true; }
int curPos = bases.size() - 1; while ((curPos >= 0) && (this->curDig.at(curPos) == this->bases.at(curPos) - 1)) { --curPos; }
if (curPos < 0) { return false; }
++this->curDig.at(curPos);
for (int right = curPos + 1; right < this->bases.size(); ++right) { this->curDig.at(right) = 0; }
++this->digCount; return true; }
inline void printCurDig() { for (const int d : this->curDig) { cout << d << " "; } cout << endl; }
inline void printDigCount() { cout << this->digCount << endl; }
private: const vector<int> bases;
vector<int> curDig; int digCount; };
int main() { const vector<int> bases{4, 5, 3};
CDigital dig(bases);
while (dig.next()) { dig.printCurDig(); } dig.printDigCount();
return 0; }
|