C语言 输入一个整数n,增序输出1,2,3,4.……n的全排列
发布网友
发布时间:2023-07-20 18:26
我来回答
共3个回答
热心网友
时间:2024-02-10 19:11
#include <stdio.h>
#define MAX 100
void process(char *c,int n)
{
int i = 0;
while(i < n)
{
printf("%c",c[i]);
i=i+1;
}
printf("\n");
}
void perm(char *list,int n)
{
int k;
char tmp;
int i = n;
int count[MAX];
count[i - 1] = 1;
while(i > 2)
{
i--;
count[i - 1] = 1;
}
process(list,n);
do{
if(count[i - 1] < i)
{
if(i%2 != 0)
k = 1;
else
k = count[i-1];
tmp = list[k - 1];
list[k-1] = list[i-1];
list[i-1] = tmp;
count[i-1] += 1;
i = 2;
process(list,n);
}
else
{
count[i-1] = 1;
i+=1;
}
}
while(i <= n);
}
int main()
{
char c[] = {'a','b','c','d'};
perm(c,4);
return 0;
}
热心网友
时间:2024-02-10 19:11
#include <stdio.h>
#define MAX 100
void process(char *c,int n)
{
int i = 0;
while(i < n)
{
printf("%c",c[i]);
i=i+1;
}
printf("\n");
}
void perm(char *list,int n)
{
int k;
char tmp;
int i = n;
int count[MAX];
count[i - 1] = 1;
while(i > 2)
{
i--;
count[i - 1] = 1;
}
process(list,n);
do{
if(count[i - 1] < i)
{
if(i%2 != 0)
k = 1;
else
k = count[i-1];
tmp = list[k - 1];
list[k-1] = list[i-1];
list[i-1] = tmp;
count[i-1] += 1;
i = 2;
process(list,n);
}
else
{
count[i-1] = 1;
i+=1;
}
}
while(i <= n);
}
int main()
{
char c[] = {'a','b','c','d'};
perm(c,4);
return 0;
}
热心网友
时间:2024-02-10 19:11
#include <stdio.h>
main()
{
int n,i;
printf("Input n:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d",i);
}
热心网友
时间:2024-02-10 19:11
不会了
热心网友
时间:2024-02-10 19:11
#include <stdio.h>
main()
{
int n,i;
printf("Input n:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d",i);
}
热心网友
时间:2024-02-10 19:12
不会了