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 }
  • 相关阅读:
    梦断代码,读书计划
    《梦断代码》读后感1
    四则运算2详细过程
    四则运算2初步构思
    四则运算
    阅读《梦断代码》计划
    四则运算2设计思路
    随机输出30道四则运算题
    软件工程阅读计划
    面试题04
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8536532.html
Copyright © 2011-2022 走看看