# 编程题AB-卡片游戏

``````3
1 4 2``````

``````6
1 2 4
1 4 2
2 1 4
2 4 1
4 1 2
4 2 1``````

``````#include <stdio.h>
#include <stdlib.h>
int a[10],b[10],c[10],n;
void dfs(int step)
{
int i;
if(step==n+1)
{
for(i=1;i<=n;i++)
{
printf(i!=n?"%d ":"%d\n",a[i]);
}
}

for(i=1;i<=n;i++)
{
if(b[i]==0)
{
a[step]=c[i];
b[i]=1;
dfs(step+1);
b[i]=0;
}
}
}
void sort(int a[],int n)
{
int i,j,t;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int sum=1,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
c[i]=a[i];
}
for(i=n;i>0;i--)
{
sum*=i;
}
printf("%d\n",sum);
sort(a,n);
sort(c,n);
dfs(1);
return 0;
}
``````

``````#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int a[12],b[12],c[12],n;
void dfs(int step){
int i;
if(step==n+1){
for(i=1;i<n;i++)printf("%d ",a[i]);
printf("%d\n",a[n]);
return ;
}
for(i=1;i<=n;++i){
if(b[i]==0){
a[step]=c[i];
b[i]=1;
dfs(step+1);
b[i]=0;
}
}
return ;
}
int main(){
while(cin>>n){
int sum=1, i;
memset(b,0,sizeof(b));
for(i=1;i<=n;++i){
cin>>a[i];
c[i]=a[i];
}
for(i=n;i>0;i--)sum*=i;
cout<<sum<<'\12';
sort(a,a+n+1);
sort(c,c+n+1);
//for(i=1;i<=n;++i)cout<<a[i]<<' ';
dfs(1);
}
return 0;
}
``````