Skip to content

Instantly share code, notes, and snippets.

@caigen
Created March 7, 2014 05:52
Show Gist options
  • Select an option

  • Save caigen/9405944 to your computer and use it in GitHub Desktop.

Select an option

Save caigen/9405944 to your computer and use it in GitHub Desktop.
一种使用进制生成全排列的方法。
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
char a[3] = { '+', '+', '+' };
int i = 0;
const int base = 4; /* [+,-,*,/] */
bool next() {
i++;
if (i >= (int)pow((double)base, 3)) {
return false;
}
int x = i;
for (int j = 0; j < 4; j++) {
int value = x % base;
x /= base;
switch (value) {
case 0:
a[j] = '+';
break;
case 1:
a[j] = '-';
break;
case 2:
a[j] = '*';
break;
case 3:
a[j] = '/';
break;
default:
return false;
}
}
return true;
}
int main(int argc, char* argv[]) {
do {
cout << a[0] << a[1] << a[2] << endl;
}
while (next());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment