zoukankan      html  css  js  c++  java
  • 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
    ",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
    ", 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
    ", count);
    61     system("pause");
    62 }
  • 相关阅读:
    jsp第六周作业
    jsp第四周作业
    jsp第一周周作业
    第一次软件测试课堂练习
    4.11jsp
    第六周作业
    第三周jsp作业
    3.10 jsp作业
    3.4软件测试
    JSP第六周作业
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8536532.html
Copyright © 2011-2022 走看看