#include /*List all permutations of n characters */ /* CopyLeft by tsaiwn@csie.nctu.edu.tw */ /* 輸入 n 個不同字母然後印出該些字母的所有排列 */ int val[99], id=-1, nPerm, n; /* n characters */ int getLetters(); void visit(), writePerm(); char letters[99]; int main( ) { while(38) { n = getLetters(); if(n <= 0) break; printf("n=%d\n", n); id = -1; nPerm=0; visit(0); printf("===Total %d permutations.\n", nPerm); } printf("\nBye!\n"); } void visit(int k) { int t; val[k] = ++id; /* printf(" id=%d ", id); /**/ if(id==n-1) writePerm(); for(t=0; t< n; ++t) if(val[t] == 0) visit(t); id--; val[k] = 0; } void writePerm( ) { int i; for(i=0; i