排列和组合算法

排列和组合算法是考查递归的常见算法,这两种算法能用递归简洁地实现。

本人在经过多次摸索和思考之后,总结如下,以供参考。

程序代码如下:


 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 char array[] = "abcd";
 5 
 6 #define N  4
 7 #define M  3
 8 int queue[N] = {0};
 9 int top = 0;
10 int flag[N] = {0};
11 
12 void perm(int s, int n)
13 {
14     int i;
15 
16     if (s > n)
17     {
18         return;
19     }
20 
21     if (s == n)
22     {
23         for (i = 0; i < n; i++)
24         {
25             printf("%c", queue[i]);
26         }
27         printf("\t");
28         return ;
29     }
30 
31     for (i = 0; i < n; i++)
32     {
33         if (flag[i] == 0)
34         {
35             flag[i] = 1;
36             queue[s] = array[i];
37             perm(s+1, n);
38             flag[i] = 0;
39         }
40     }
41 }
42 
43 void comb(int s, int n, int m)
44 {
45     int i;
46 
47     if (s > n)
48         return ;
49 
50     if (top == m)
51     {
52         for (i = 0; i < m; i++)
53         {
54             printf("%c", queue[i]);
55         }
56         printf("\t");
57         return ;
58     }
59 
60     queue[top++] = array[s];
61     comb(s+1, n, m);
62     top--;
63     comb(s+1, n, m);
64 
65 }
66 
67 int main()
68 {
69     printf("\nperm():\n");
70     perm(0, N);
71     printf("\ncombination():\n");
72     comb(0, N, M);
73     printf("\n");
74     return 0;

75 }


运行结果:

perm():
abcd    abdc    acbd    acdb    adbc    adcb    bacd    badc    bcad    bcda
bdac    bdca    cabd    cadb    cbad    cbda    cdab    cdba    dabc    dacb
dbac    dbca    dcab    dcba
combination():
abc     abd     acd     bcd
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中实现排列组合算法有多种方法。引用提到了一种递归实现的方法,这是一种最优解,也是最常见的方法。可以参考该博客中的代码来实现排列组合算法。 此外,还可以使用循环来实现排列组合算法。可以使用嵌套循环来生成所有可能的组合。外层循环用于选择第一个元素,内层循环用于选择剩余的元素。通过使用标记数组来跟踪已选择的元素,可以避免生成重复的组合。 以下是一个简单示例,展示了如何使用循环实现排列组合算法: ```java public class Combination { public static void main(String[] args) { String[] elements = {"A", "B", "C"}; int length = 2; generateCombinations(elements, length); } public static void generateCombinations(String[] elements, int length) { int[] combination = new int[length]; generateCombinationsUtil(elements, combination, 0, 0); } public static void generateCombinationsUtil(String[] elements, int[] combination, int current, int start) { if (current == combination.length) { for (int i : combination) { System.out.print(elements[i + " "); } System.out.println(); } else { for (int i = start; i < elements.length; i++) { combination[current = i; generateCombinationsUtil(elements, combination, current + 1, i + 1); } } } } ``` 该示例中,我们选择了元素数组{"A", "B", "C"}并设置了组合的长度为2。然后,通过调用`generateCombinations`方法生成所有可能的组合。`generateCombinationsUtil`方法使用嵌套循环遍历所有可能的组合,并打印出结果。 请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值