user-0 提问: 使用递归来实现全排列（有限定条件）

回复:
user: user-1
content: 仅供参考//qplw.cpp #include <stdio.h> #include <string.h> #include <stdlib.h> int v=0; int w=0; int m;//记录字符串长度 int n;//记录字符串中的字符种类数 char map[256];//记录是哪几种字符 int count[256];//记录每种字符有多少个 int stack[1000];//递归用的栈，并记录当前生成的排列 void Make_Map(char *str) {//统计字符串的相关信息     int s[256];     int i;     memset(s,0,sizeof(s));     memset(count,0,sizeof(count));     m=strlen(str);     if (w<1 || m<w) w=m;     while(*str) {         s[*str]++;         str++;     }     n=0;     for (i=0;i<256;i++)         if (s[i]) {             map[n]=i;             count[n]=s[i];             n++;         } } void Find(int depth) {//递归式回溯法生成全排列     if (depth==w) {         int i;         for (i=0;i<depth;i++) putchar(map[stack[i]]);         putchar('\n');     } else {         int i;         if (v && depth>0) {             for (i=0;i<depth;i++) putchar(map[stack[i]]);             putchar('\n');         }         for (i=0;i<n;i++)             if (count[i]) {                 stack[depth]=i;                 count[i]--;                 Find(depth+1);                 count[i]++;             }     } } void main(int argc,char**argv) {     if (argc<2) {         printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);         return;     }     if (argc>=3) w=atoi(argv[2]);     if (-1==w) v=1;     Make_Map(argv[1]);     Find(0); } //C:\test>qplw //qplw 要产生全排列的字符串 [限定长度|-1] // //C:\test>qplw 123 //123 //132 //213 //231 //312 //321 // //C:\test>qplw 123 2 //12 //13 //21 //23 //31 //32 // //C:\test>qplw 122333 3 //122 //123 //132 //133 //212 //213 //221 //223 //231 //232 //233 //312 //313 //321 //322 //323 //331 //332 //333 // //C:\test>qplw 123 -1 //1 //12 //123 //13 //132 //2 //21 //213 //23 //231 //3 //31 //312 //32 //321 //

user: user-2
content: /* 数组：int s[4] = {1,2,3,4}; 利用递归来实现数组s的全排列组合，并且4必须在3的左面，例如xx43或x43x或x4x3等。 */  #include <stdio.h> #include <stdlib.h>  #define MAX 4  int genarate(int a[], int pos, int p3, int p4);  int main(int argc, char *argv[]) { 	int s[MAX] = { 1, 2, 3, 4}; 	genarate(s, 0, -1, -1); 	return 0;	 }  void swap(int *a, int *b) { 	int t ; 	t=*a; 	*a=*b; 	*b=t; }  int genarate(int a[], int pos, int p3, int p4) { 	int i; 	if (p3>-1 && p4>-1 && p3<p4) return 0; 	 	if (pos>=MAX) { 		for(i=0; i<MAX; i++) { 			printf("%d %c", a[i], i==MAX-1? '\n':','); 		} 	} else { 		for(i=pos;i<MAX;i++){ 			swap(a+pos, a+i); 			genarate(a, pos+1, a[pos]==3?pos:p3, a[pos]==4?pos:p4); 			swap(a+pos, a+i); 		} 	} 	return 0; }

user: user-3
content: 献丑了。。。 #include <stdio.h> #include <string.h>  #define MAX 4  char str[MAX+1]="    "; int num[MAX+1]={0};  void funiction(int len) { 	int i; 	if(len==0) 	{ 		puts(str); 		return ; 	} 	for(i=0;i<MAX;i++) 	{ 		if(!num[i]) 		{ 			num[i]=1; 			str[MAX-len]='1'+i; 			if(num[3]-num[2]>=0)funiction(len-1); 			num[i]=0; 		} 	} }  int main() { 	funiction(4); 	return 0; }

user: user-4
content: #include <stdio.h>  int &swap(int &x,int&y) {        int z;       if(x<y)       {                z=x;                x=y;                y=z;      }      return x; }  void main() {          int s[4]={1,2,3,4};          int a=swap(swap(swap(s[0],s[1]),s[2]),s[3]);          printf("%s\n",s);  }   输出结果是 4 1 3 2，改变实参的参数可以实现别的排列， 你自己可以调试一下

user: user-5
content: 不会啊，等赵老师来吧

user: user-6
content: 初学C，有什么写得不好的地方，你来咬我啊 #include <stdio.h> #include <stdbool.h> #include <string.h> #include <limits.h> #include <stdlib.h> #ifdef _MSC_VER #define printf printf_s #define snprintf sprintf_s #else #define strcpy_s(DEST,DESTEC,SRC) strcpy((DEST), (SRC)) #define __inline #endif typedef bool(*ValidatorFunc)(const char *);  bool Validate(const char * str) {     int pos3 = INT_MAX;     int pos4 = -1;     int poss = -1;     int cur = 0;     while (*str != '\0')     {         if (poss == cur - 1)         {             if (*str == '3' && (*(str + 1) == ' ' || *(str + 1) == '\0'))             {                 pos3 = cur;             }             else if (*str == '4' && (*(str + 1) == ' ' || *(str + 1) == '\0'))             {                 pos4 = cur;             }         }         else if (*str == ' ')         {             poss = cur;         }         cur++;         str++;     }     return pos4 < pos3; }  typedef void(*EnumArrangeCallback)(const char *);  void PrintOutString(const char * str) {     printf("%s\n", str); }  __inline void SwapInt(int * array, int index1, int index2) {     int temp;     temp = array[index1];     array[index1] = array[index2];     array[index2] = temp; }  void EnumPremutations(char * template, int * nums, size_t numcount, EnumArrangeCallback callback, ValidatorFunc validator) {     if (numcount == 0)     {         if (validator && validator(template))         {             callback(template);         }     }     bool nulltmpl = false;     if (template == NULL)     {         template = (char *)malloc(numcount * 12 /* 12 = strlen("-2147483648") + 1 //1 for space*/);         memset(template, 0, numcount * 12);         nulltmpl = true;     }     size_t tmplend = strlen(template);     char * tmplcpy = NULL;     if (tmplend > 0)     {         tmplcpy = malloc(tmplend + 1);         strcpy_s(tmplcpy, tmplend + 1, template);     }     for (size_t i = 0; i < numcount; i++)     {         if (tmplend > 0)         {             snprintf(template, tmplend + numcount * 12, "%s %d", tmplcpy, nums[i]);         }         else         {             snprintf(template, numcount * 12, "%d", nums[i]);         }         if (validator && validator(template))         {             SwapInt(nums, i, numcount - 1);             EnumPremutations(template, nums, numcount - 1, callback, validator);             SwapInt(nums, i, numcount - 1);         }         template[tmplend] = '\0';     }     if (nulltmpl == true)     {         free(template);     } }  int main(void) {     int src[] = { 1, 2, 3, 4 };     EnumPremutations(NULL, src, sizeof(src) / sizeof(*src), &PrintOutString, &Validate);     getchar();     return EXIT_SUCCESS; }