1. 输出自然数1到n所有不重复的排列,即n的全排列。(用递归算法编写)
展开全部
#include <stdio.h>
#define SWAP(a, b) {\
int t = a;\
a = b;\
b = t;\
}
void permutation(int* a, int b, int e) {
int i;
if (b == e) {
for (i = 0; i < e; ++i) {
printf("%d ", a[i]);
}
printf("\n");
} else {
for (i = b; i < e; ++i) {
SWAP(a[b], a[i]);
permutation(a, b + 1, e);
SWAP(a[b], a[i]);
}
}
}
int main(void) {
int a[4] = {1,2,3,4};
permutation(a, 0, 4);
/*system("pause");*/
return 0;
}
#define SWAP(a, b) {\
int t = a;\
a = b;\
b = t;\
}
void permutation(int* a, int b, int e) {
int i;
if (b == e) {
for (i = 0; i < e; ++i) {
printf("%d ", a[i]);
}
printf("\n");
} else {
for (i = b; i < e; ++i) {
SWAP(a[b], a[i]);
permutation(a, b + 1, e);
SWAP(a[b], a[i]);
}
}
}
int main(void) {
int a[4] = {1,2,3,4};
permutation(a, 0, 4);
/*system("pause");*/
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询