本帖最后由 jhq999 于 2022-5-10 12:04 编辑 void qusu(int (*num)[2],int *box,int n,int m,int *count)
{
int i;
if(0==m)
{
for (i = 0; box[i]; i++);
for (i = i - 1; i >= 0 ; i--)
{
printf("%d",box[i]);
}
(*count)++;
printf("\n");
return;
}
for (i = 0; i < n; i++)
{
if (0==num[i][0])
{
box[m-1]=num[i][1];
num[i][0]=1;
qusu(num,box,n,m-1,count);
num[i][0]=0;
}
}
}
int main()
{
int n,m,count=0;
scanf("%d%d",&n,&m);
int (*num)[2]=new int[n][2];
int *box=new int[m+1];
for (int i = 0; i < n; i++)
{
num[i][0]=0;
num[i][1]=i+1;
}
box[m]=0;
qusu(num,box,n,m,&count);
printf("\n%d",count);
delete[] num;
delete[] box;
return 0;
}
void qusu(int (*num)[2],int *box,int m,int *count)
{
int i;
if(0==m)
{
for (i = 0; box[i]; i++);
for (i = i - 1; i >= 0 ; i--)
{
printf("%d",box[i]);
}
(*count)++;
printf("\n");
return;
}
for (i = 0; num[i][1]; i++)
{
if (0==num[i][0])
{
box[m-1]=num[i][1];
num[i][0]=1;
qusu(num,box,m-1,count);
num[i][0]=0;
}
}
}
int main()
{
int n,m,count=0;
scanf("%d%d",&n,&m);
int (*num)[2]=new int[n+1][2];
int *box=new int[m+1];
for (int i = 0; i < n; i++)
{
num[i][0]=0;
num[i][1]=i+1;
}
num[n][1]=num[n][0]=0;
box[m]=0;
qusu(num,box,m,&count);
printf("\n%d",count);
delete[] num;
delete[] box;
return 0;
}
|