1到8的全排列
#include <iostream> #include <cstring> #include <string> #include<algorithm> using namespace std; int v[10]; int a[10]; void display() { int i; for (i = 1; i <= 8; i++) { if (i == 8) cout << a[i] << endl; else cout << a[i] << " "; } } void dfs(int s) { int i; if (s == 9) display(); for (i = 1; i <= 8; i++) { if (v[i]==0) { v[i] = 1; a[s] = i; dfs(s+1); v[i] = 0; } } } int main() { memset(v, 0, sizeof(v)); dfs(1); return 0; }