全排列的程序似乎很基础,不过乍一想还真有点儿想不通,防止忘记,存下来作为积累吧。#include<stdio.h>
#define N 4
void swap(int *first, int *second)
{int temp;
temp = *first;
*first = *second;
*second = temp;
}
void QuanPaiLie(int array[], int start, int end)
{int current;
if(start > end)
{for(current = 0; current <= end; current ++) printf("%d ", array[current]);
printf("\n");
}
else
{for(current = start; current <= end; current ++)
{swap(&array[current], &array[start]);
QuanPaiLie(array, start + 1, end);
swap(&array[current], &array[start]);
}
}
}
void main(void)
{int arraylist[N], iter;
printf("Please input %d numbers for test: ", N);
for(iter = 0; iter < N; iter ++)
scanf("%d", & arraylist[iter]);
printf("Now the result is:\n");
QuanPaiLie(arraylist, 0, N - 1);
}
评论