不考虑数组元素相同的情况~!
2、定义一个数组,编程打印它的全排列。比如定义:
#define N 3
int a[N] = { 1, 2, 3 };
则运行结果是:
$ ./a.out
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2
#include <stdio.h>
#define N 3
int a[N];
void perm(int);
void print();
void swap(int, int);
int main(){
int i;
for(i = 0; i < N; ++i){
a[i] = i + 1;
}
perm(0);
}
void perm(int offset){
int i, temp;
if(offset == N-1){
print();
return;
}else{
for(i = offset;i < N; ++i){
swap(i, offset);
perm(offset + 1);
swap(i, offset);
}
}
}
void print(){
int i;
for(i = 0; i < N; ++i)
printf(" %d ",a[i]);
printf("\n");
}
void swap(int i, int offset){
int temp;
temp = a[offset];
a[offset] = a[i];
a[i] = temp;
}
|