69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include<string.h>
void Permutation(char list[], int k, int m)
{
if (k == m)
{
printf("%s\n",list);
}
else
{
char s[10];
int i;
for ( i=k; i <= m; i++)
{
int t;
t=list[k],list[k]=list[i],list[i]=t;
strcpy(s,list+k);
Permutation(list, k+1, m);
strcpy(list+k,s);
}
}
}
void main()
{
char s[]="1234";
Permutation(s, 0, 3);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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);
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]]);
putchar('\n');
} else {
int i;
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 要产生全排列的字符串\n",argv[0]);
return;
}
Make_Map(argv[1]);
Find(0);
}
#include<stdio.h>
void main()
{
int a[4],i,j,m,n,k=0;
printf("please in put four numbers :");
for(i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(m=0;m<4;m++)
{
for(n=0;n<4;i++)
{
if(a[i]!=a[j]&& a[i] != a[m] && a[i] != a[n] && a[j] != a[n] && a[j] != a[n] && a[m] != a[n])
{
printf("%d%d%d%d ",a[i],a[j],a[m],a[n]);
k++;
if(k%4==0)
printf("\n");
}
}
}
}
}
#include<stdio.h>
void main()
{
int a[4],i,j,m,n,k=0;
printf("please in put four numbers :");
for(i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(m=0;m<4;m++)
{
for(n=0;n<4;n++)
{
printf("%d%d%d%d ",a[i],a[j],a[m],a[n]);
k++;
if(k%4==0)
printf("\n");
}
}
}
}
printf("排列种数 :%d",k);
}