//STL...一句话而已,不用多讲..速度还可以 #include <iostream> #include <algorithm> #define max 1000 using namespace std; typedef struct ybk{ int x,y;}Point; Point flag[max]; int n; bool cmp(Point at,Point bt){ return at.x<bt.x;} int main() { int i,j,k; cin>>n; for(i=0; i<n; i++) { flag[i].x = 2*i; flag[i].y = 2*i+1;} do{ for(i=0; i<n; i++) cout<<"("<<flag[i].x<<" "<<flag[i].y<<") "; cout<<endl; }while(next_permutation(flag,&flag[n],cmp)); system("pause"); } /..按字典序生产算法自己code...ms速度很难比得上STL #include <iostream> #define maxL 1000 using namespace std; int flag[maxL]; int next_permutation(int flag[],int n) { int i,j; for(i=n-1; i>0; i--) if(flag[i]>flag[i-1]) break; if(i==0) return 0; for(j=n-1; j>0; j--) if(flag[j]>flag[i-1]) break; flag[i-1]^=flag[j]^=flag[i-1]^=flag[j];//swap j = n-1; while(i<j){ flag[i]^=flag[j]^=flag[i]^=flag[j]; i++; j--; } return 1; } int main() { int i,j,n; cin>>n; for(i=0; i<n; i++) flag[i] = i; do{ for(i=0; i<n; i++) cout<<flag[i]<<" "; cout<<endl; }while(next_permutation(flag,n)); system("pause"); return 0; } /// ============= track back: http://blog.chinaunix.net/u/23273/showart_187832.html =============