Created
March 7, 2014 05:52
-
-
Save caigen/9405944 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 <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