void print_permutation(int n, int h, int pos) { if( pos==n ) { for( int i = 0 ; i!=n ; ++i ) { printf("%d",out[i]); } printf("\n"); } else { int usedzero=0,usedone=0; int numone=h,numzero=n-h; for( int i = 0 ; i!=pos ; ++i ) { if( out[i]==1 ) { usedone++; } else usedzero++; } if( usedzero < numzero ) { out[pos]=0; print_permutation(n, h, pos+1); } if( usedone < numone ) { out[pos]=1; print_permutation(n,h,pos+1); } } }
void DFS(int index)/*index->x used->y*/ { if(index == 9) { int tmp_max = print_permutation(); if(tmp_max > max) { max = tmp_max; } return; } int a; for(a = 1; a <= 8 ; a++) /*a->y*/ { if(isValid(index, a)) { board[index][0] = index; board[index][1] = a; used[a] = 1; DFS (index+1); used[a] = 0; } } }
void print_permutation(int n, int length, char* permutation) { if( n==length ) { permutation[n]='\0'; printf("%s\n",permutation); } else for( int i = 0 ; i!=length ; ++i ) { if( i==0 || input[i]!=input[i-1] ) { int used=0,num=0; for( int j = 0 ; j!=length ; ++j ) { if( input[j]==input[i] ) { num++; } } for( int j = 0 ; j!=n ; ++j ) { if( permutation[j]==input[i] ) { used++; } } if( used<num ) { permutation[n]=input[i]; print_permutation(n+1, length, permutation); } } } }
void print_permutation(char *wordRa){ static count; char singleWord[2]; char aheadWord[10],currentwordRa[10]; int index,wordRalength,i; wordRalength=strlen(wordRa); if(wordRalength>2){ strcpy(currentwordRa,wordRa); for(i=0;i<wordRalength;i++){ singleWord[0]=currentwordRa[0]; singleWord[1]='\0'; strcat(toBePrinted,singleWord); strcpy(aheadWord,currentwordRa+1); print_permutation(aheadWord); index=strlen(toBePrinted)-1; toBePrinted[index]='\0'; rotateLeft(currentwordRa); } }else { //printf("\n%d)TOBEpTelse=%s",++count,toBePrinted); printf("\n%s",toBePrinted); printf("%s",wordRa); //printf("\n%d)TOBEpTelse=%s",++count,toBePrinted); printf("\n%s",toBePrinted); printf("%c%c",wordRa[1],wordRa[0]); } }
int main(){ memset(vis, 0, sizeof(vis)); int n, A[MAXN]; scanf("%d",&n); print_permutation(n, A, 0); printf("n=%d,way2 time used: %.2lf\n",n, (double)clock()/CLOCKS_PER_SEC); return 0; }
main(){ char word[20]; scanf("%s",word); //gets(word); //rotateLeft(word); //printf("%s",word); print_permutation(word); }
int main(){ int n; scanf("%d",&n); for(int i = 0 ;i < n;i ++) { scanf("%d",&b[i]); } print_permutation(n,b,a,0); return 0 ; }
void print_permutation(int n, int A[], int cur){ // cur == current if(cur == n) { for(int i=0; i<n; i++) printf("%d ",A[i]); putchar('\n'); } else for(int i=1; i<=n; i++) if(!vis[i]){ vis[i] = 1; A[cur] = i; print_permutation(n, A, cur+1); vis[i] = 0; } }
int main(int argc, char *argv[]) { int n,h,num; scanf("%d",&num); while( num-- ) { scanf("%d %d",&n,&h); print_permutation(n,h,0); if(num) printf("\n"); } }
int main(int argc, char *argv[]) { int n; scanf("%d\n",&n); while( n-- ) { fgets(input, 20, stdin); int length = strlen(input)-1; std::sort(input, input+length); print_permutation(0, length, permutation); printf("\n"); } }
int main(){ freopen("722.in", "r", stdin); freopen("722time.txt", "a", stdout); int n, A[MAXN], P[MAXN]; scanf("%d",&n); for(int i = 0; i < n; i++) scanf("%d",&P[i]); //n = 4; std::sort(P,P+n); //for(int i= 0;i< n;i++) printf("%d ",P[i]); print_permutation(n, P, A, 0); printf("n=%d, way1 time used: %.4lfs\n",n, (double)clock()/CLOCKS_PER_SEC); return 0; }
// 输出1~n的全排列 void print_permutation(int n, int* A, int cur) { int i, j; if(cur == n) { // 递归边界 for(i = 0; i < n; i++) printf("%d ", A[i]); printf("\n"); } else for(i = 1; i <= n; i++) { // 尝试在A[cur]中填各种整数i int ok = 1; for(j = 0; j < cur; j++) if(A[j] == i) ok = 0; // 如果i已经在A[0]~A[cur-1]出现过,则不能再选 if(ok) { A[cur] = i; print_permutation(n, A, cur+1); // 递归调用 } } }
void print_permutation(int n, int P[], int A[], int cur){ if(cur == n) { for(int i=0; i<n; i++) ;//printf("%d ",A[i]); //putchar('\n'); } else for(int i=0; i<n; i++) if(!i || P[i] != P[i-1]) { //if的意�? 对于重复元素(排序后已经挨在一起),只试它们中的第一个,其余跳过 //P[0]是必然要试的:若有多个等于P[0]的值,P[0]必为第一�?0-1=-1不在数组下表范围�?;若只有一个,更理所当然地要试P[0]�?!i即为i==0) int c1 = 0, c2 = 0; for(int j=0; j<cur; j++) if(A[j] == P[i]) c1++; for(int j=0; j<n; j++) if(P[j] == P[i]) c2++; if(c1 < c2) { A[cur] = P[i]; print_permutation(n, P, A, cur+1); } } }
int main(int argc, char* argv[]) { // input uint64_t r; // desired rank uint32_t n; // length of set uint32_t k; // length of permutation uint32_t* p; // identity permutation uint32_t* unranked; // unranked permutation if (argc < 3 || argc > 4) { printf("Usage: %s r n <k>\n", argv[0]); return 1; } r = strtoull(argv[1], (char**)NULL, 10); n = strtoul(argv[2], (char**)NULL, 10); if (argc == 4) { k = strtoul(argv[3], (char**)NULL, 10); } else { k = n; } if (k > n) { printf("Error: value of k must be less than or equal to n.\n"); return 1; } p = create_identity_permutation(n); unranked = unrank(n, k, r, p); print_permutation(k, unranked); free(p); return 0; }
void print_permutation(int *A,int cur,int n){ if(cur==n){ for(int i=0;i<n;i++){ printf("%d ",A[i]); } printf("\n"); }else{ for(int i=1;i<=n;i++){ bool ok = true; for(int j=0;j<cur;j++){ if(A[j]==i){ ok = false; break; } } if(ok){ A[cur] = i; print_permutation(A,cur+1,n); } } } }
//freopen("/home/plac/problem/input.txt","r",stdin); //freopen("/home/plac/problem/output.txt","w",stdout); void print_permutation(int n,int *P,int *A,int cur) { int i , j; if(cur == n) { for(int i = 0 ;i < n ;i ++) printf("%d ",A[i]); printf("\n"); }else for(i = 0;i < n;i ++) if(!i || P[i] != P[i-1]) { int c1 = 0 ,c2 = 0; for(j = 0 ; j < cur ; j ++) if(A[j] == P[i]) c1 ++ ; for(j = 0 ;j < n ; j ++) if(P[j] == P[i]) c2++; if(c1 < c2) { A[cur] = P[i]; print_permutation(n,P,A,cur+1); } } }
int main() { int n; scanf("%d",&n); print_permutation(A,0,n); return 0; }
int main() { print_permutation(4, A, 0); return 0; }