不知道怎么对齐,是C语言的基础,好长时间没复习不知道怎么弄了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
int t;
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
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==m) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
if (t%5==4) putchar('\n'); else putchar(' ');
t++;
} else {
int i;
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main() {
t=0;
Make_Map("1234");
Find(0);
}
//1234 1243 1324 1342 1423
//1432 2134 2143 2314 2341
//2413 2431 3124 3142 3214
//3241 3412 3421 4123 4132
//4213 4231 4312 4321