-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateFullPermutation.cpp
65 lines (59 loc) · 1.56 KB
/
CreateFullPermutation.cpp
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <stack>
#include <string>
#include <string.h>
#include <assert.h>
#include <fstream>
#include <filesystem>
#include <algorithm>
#include "common.h"
using namespace std;
void CreateFullPermutation::init(const vector<int>& numArray) {
mNumArray = numArray;
}
void CreateFullPermutation::getFirst(vector<int>& nextNumArray) {
mLast.resize(mNumArray.size(), 0);
nextNumArray.assign(mLast.begin(), mLast.end());
}
bool CreateFullPermutation::getNext(vector<int>& nextNumArray) {
// from the low digit, add 1 each time.
int i = mLast.size() - 1;
for (; i >= 0; i--) {
mLast[i] += 1;
if (mLast[i] == mNumArray[i]) {
// ½øλ
mLast[i] = 0;
} else {
// find the next
nextNumArray.assign(mLast.begin(), mLast.end());
return true;
}
}
if (i < 0) {
// return value should not be used
return false; // no more to return
}
assert(false); // should not reach here
return false;
}
void CreateFullPermutation::testOne(const vector<int>& oneData) {
cout << "testOne" << endl;
Tools::print(oneData);
CreateFullPermutation creator;
creator.init(oneData);
vector<int> numArray;
creator.getFirst(numArray);
Tools::print(numArray);
while (creator.getNext(numArray)) {
Tools::print(numArray);
}
}
void CreateFullPermutation::test() {
testOne({1, 1, 1});
testOne({3, 3, 3});
testOne({3, 2, 1});
testOne({2, 4, 3, 2});
}