27.全排列与带重复的排列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define LEN 4 //标识从所有数据中选出几个
 5 
 6 //保存结果
 7 char res[LEN+1] = { 0 };
 8 //保存有多少个
 9 int count = 0;
10 
11 //全排列(有重复)
12 //j为当前位置
13 void showall(char *p , int j)
14 {
15     if (j == LEN)
16     {
17         count++;
18         printf("%s\n",res);
19         return;
20     }
21 
22     for (int i = 0; i < LEN; i++)
23     {
24         res[j] = p[i];
25         showall(p,j+1);
26     }
27 }
28 
29 //判断第i个数据有没有用过,如果用过则跳过
30 int appear[LEN] = { 0 };
31 //全排列(无重复)
32 void none_showall(char *p, int j)
33 {
34     if (j == LEN)
35     {
36         count++;
37         printf("%s\n", res);
38         return;
39     }
40 
41     for (int i = 0; i < LEN; i++)
42     {
43         if(appear[i]==0)
44         {
45             //标识已经用过
46             appear[i] = 1;
47             res[j] = p[i];
48             none_showall(p, j + 1);
49             appear[i] = 0;
50         }
51     }
52 }
53 
54 
55 void main()
56 {
57     char *items = "1234";
58     //showall(items, 0);
59     none_showall(items, 0);
60     printf("一共有多少种:%d\n", count);
61     system("pause");
62 }

 

posted @ 2018-03-09 21:49  喵小喵~  阅读(149)  评论(0编辑  收藏  举报