产生所有排列

        我觉得这是一个很巧秒的算法。思路非常直接,从代码里可以很容易看出来,再单步调试查看set数组的值就可以很清楚地明白算法的过程。

        代码如下:

 1 #include <stdio.h>
 2 #define MAX 1000
 3 
 4 int n=3;  //the number of set element
 5 int set[MAX]={1,2,3};
 6 
 7 //move the set[0] to set[position]
 8 int rotate(int position)
 9 {
10     int temp=set[0]; 
11     int index;
12     for(index=1;index<=position;index++)
13         set[index-1]=set[index];
14     set[position]=temp;
15 }
16 
17 void set_print()
18 {
19     int index;
20     for(index=0;index<n;index++)
21         printf("%d ",set[index]);
22     printf("\n");
23 }
24 int main()
25 {
26     int position=n-1;
27     while(position!=0)  
28     {
29         position=n-1; 
30         rotate(position);
31         set_print();
32         while(set[position]==position+1 && position!=0)
33         {
34             position--; 
35             rotate(position);
36         }
37     }
38 }

        参考资料:《C语言名题精选百则技巧篇》

        如果你觉得我的文章对你有帮助,请推荐一下,非常感谢!


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值