c语言问题:盘输入一个仅由小写字母组成的字符串,输出以该串中任 取...
发布网友
发布时间:2024-10-22 19:17
我来回答
共1个回答
热心网友
时间:2024-10-22 19:32
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
static int v[1024];//访问数组
static char t[1024][1024], a[1024];
static int j = 0, count = 0;
void pailie(char *s, int M, int k)
{
int i;
if (j != M)
{
for (i = 0; i < strlen(s); i++)
{
if (v[i] == 0)
{
v[i] = 1;
a[j] = s[i];
j++;
pailie(s, M, i);
}
}
}
else
{
a[j] = '\0';
for (i = 0; i < count; i++)
{
if (strcmp(t[i], a) == 0)i = count + 1;
}
if (i == count)
{
strcpy(t[count], a);
count++;
}
}
j--;
v[k] = 0;
}
int main()
{
char s[1024];
int M;
printf("请输入字符串:");
gets(s);
printf("\n请输入所取字符个数M:");
scanf("%d", &M);
for (int i = 0; i < strlen(s); i++)
v[i] = 0;
pailie(s, M, 0);
printf("\n得到的排列总数为:%d\n", count);
printf("\n得到的所有排列为:\n");
for (int i = 0; i < count; i++)
printf("%s\t", t[i]);
printf("\n\n");
system("pause");
}