Created
October 13, 2013 14:30
-
-
Save caigen/6962971 to your computer and use it in GitHub Desktop.
百度2014软件研发笔试题算法题1(上海站)
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 <cstdlib> | |
| #include <iostream> | |
| using std::cout; | |
| using std::endl; | |
| const int NUM = 4; | |
| int data[NUM]; | |
| void init(int data[], int n) { | |
| for (int i = 0; i<= n-1; i++) { | |
| data[i] = i; | |
| } | |
| } | |
| void print(int data[], int n) { | |
| for (int i = 0; i <= n-2; i++) { | |
| cout << data[i] << " "; | |
| if ((i+1)%10 == 0) { | |
| cout << endl; | |
| } | |
| } | |
| cout << data[n-1] << endl; | |
| } | |
| template <typename T> | |
| void swap(T &a, T &b) { | |
| T temp = a; | |
| a = b; | |
| b = temp; | |
| } | |
| // output the combinations. | |
| void print_comb(int data[], int n) { | |
| for (int i = 0; i <= n-1; i++) { | |
| print(data,i+1); | |
| for (int j = 0; j <= i; j++) { | |
| for (int k = i+1; k <= n-1; k++) { | |
| swap(data[j], data[k]); | |
| print_comb(data, i+1); | |
| swap(data[j], data[k]); | |
| } | |
| } | |
| } | |
| } | |
| int main(int argc, char* argv[]) { | |
| init(data, NUM); | |
| print_comb(data, NUM); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment