这个算法,弄头疼了 一上午还是对其中递归是抹零两可的。
论坛地址:http://topic.youkuaiyun.com/u/20091017/11/57e2123c-04f2-431d-be02-f3485fbb8b24.html
代码如下:
// test1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; #include <stdio.h> int n = 0; void swap(int *a, int *b) { int m; m = *a; *a = *b; *b = m; } void perm(int list[], int k, int m) { int i; if(k > m) { for(i = 0; i <= m; i++) printf("%d ", list[i]); printf("\n"); n++; } else { for(i = k; i <= m; i++) { swap(&list[k], &list[i]); perm(list, k + 1, m); swap(&list[k], &list[i]); } } } int main() { int list[] = {1, 2, 3, 4, 5}; perm(list, 0, 4); printf("total:%d\n", n); return 0; }
递归排列算法解析
本文通过一个具体的递归排列算法示例,详细介绍了如何使用递归实现数组元素的所有可能排列。该算法首先定义了一个递归函数perm来生成排列,并通过交换数组元素的位置来产生不同的组合。文中提供了一个完整的C++代码示例,展示了从初始化数组到输出所有排列结果的全过程。
2952

被折叠的 条评论
为什么被折叠?



