#include<stdio.h>
#define NUMBER 5
FILE * fp;
void print_array(int p[])
{
int i;
for(i=0;i<NUMBER;i++)
{
printf("%d ",p[i]);
fprintf(fp,"%d ",p[i]);
}
fprintf(fp,"/n");
printf("/n");
}
void print_array_fp(int p[])
{
int i;
for(i=0;i<NUMBER;i++)
{
fprintf(fp,"%d ",p[i]);
}
fprintf(fp,"/n");
}
void swap(int &x,int &y)
{
int temp = x;
x = y;
y = temp;
}
//用递归
void f(int p[], int start, int end)
{
int i;
if (start == end)
print_array(p); //打印p数组的值
else
{
for(i = start; i <= end; i++)
{
fprintf(fp,"i = %d, start = %d, end = %d then swap1 p[start], p[i] : ", i,start,end);
swap(p[start], p[i]); //swap交换两变量的值
print_array_fp(p);
fprintf(fp,"f(%d,%d) /n",start+1,end);
f(p, start+1, end);
fprintf(fp,"i = %d, start = %d, end = %d then swap2 p[start], p[i] : ", i,start,end);
swap(p[start], p[i]);
print_array_fp(p);
}
}
}
void main()
{
fp = fopen("c://result.txt","w+t");
int data[NUMBER] = {1,2,3,4,5};
f(data, 0, 4);
}