33,010
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
string List[3][7]={"a1", "a2", "a3", "a4", "a5", "a6", "a7",
"b1", "b2", "b3", "b4", "b5", "b6", "b7",
"c1", "c2", "c3", "c4", "c5", "c6", "c7"
};
void f( int n, int m ){
int *A=new int[n+1], *B=new int[m+1], *T=new int[m+1];
int i, j, h, line=sizeof(List[0])/sizeof(List[0][0]);
for( i=1; i <=n; ++i ) A[i]=i-1;
for( i=1; i <=m; ++i ) B[i]=m;
while( B[1] <n ){
for( h=0; h<m+1; ++h ) T[h]=A[h];
sort( T+1, T+m+1 );
do{
for( h=1; h<m+1; ++h )
cout<<List[T[h]/line][T[h]%line]<<" ";
cout<<endl;
}while( next_permutation( T+1, T+m+1 ) );
j=m;
while( B[j]==n )
--j;
++B[j];
for( i=j+1; i <=m; ++i )
B[i]=B[j];
swap( A[j], A[B[j]] );
}
for( h=0; h<m+1; ++h ) T[h]=A[h];
sort( T+1, T+m+1 );
do{
for( h=1; h<m+1; ++h )
cout<<List[T[h]/line][T[h]%line]<<" ";
cout<<endl;
}while( next_permutation( T+1, T+m+1 ) );
delete []A; delete []B; delete []T;
}
int main(int argc, char *argv[])
{
int k;
cin>>k;
f( sizeof(List)/sizeof(List[0][0]), k );
return 0;
}