编程序写出1到9的全排列
编写程序写出1到9的全排列,各位都有什么好的想法
http://zhidao.baidu.com/link?url=w9uOsBFmhzh5NRGep-D4XK9GGIF5JO5z7_-IDTJO53oeM2gxcH6WzNuMmRMd_JKK0sYJZrJ23f-sbomYdjgiEa
程序代码:
#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, 6, 7, 8, 9}; perm(list, 0, 8); printf("total:%d\n", n); return 0; }