正文

全排列2011-01-13 22:25:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/tld5yj/52160.html

分享到:

全排列的程序似乎很基础,不过乍一想还真有点儿想不通,防止忘记,存下来作为积累吧。#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);
}

阅读(1860) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册