zoukankan      html  css  js  c++  java
  • 字符的排列

    问题:输入一字符串(要求不存在重复字符),打印出该字符串中字符中字符的所有排列。

    思路:求所有可能出现在第一个位置的字符,把第一个字符和其后面的字符一一交换。固定第一个字符,求后面所有字符的排列。这个时候扔把后面的所有字符分成两部分:后面字符的第一个字符,以及这个字符之后的所有字符,然后把第一个字符逐一和它后面的字符交换。

     1 #include<stdio.h>
     2 #include "stdafx.h"
     3 #include<tchar.h>
     4 
     5 void Permutation(char* pStr, char* pBegin);
     6 
     7 void Permutation(char* pStr)
     8 {
     9     if(pStr == NULL)
    10         return;
    11 
    12     Permutation(pStr, pStr);
    13 }
    14 
    15 void Permutation(char* pStr, char* pBegin)
    16 {
    17     if(*pBegin == '')
    18     {
    19         printf("%s
    ", pStr);
    20     }
    21     else
    22     {
    23         for(char* pCh = pBegin; *pCh != '' ; ++pCh)
    24         {
    25             char temp = *pCh;
    26             *pCh = *pBegin;
    27             *pBegin = temp;
    28 
    29             Permutation(pStr, pBegin + 1);
    30 
    31             temp = *pCh;
    32             *pCh = *pBegin;
    33             *pBegin = temp;
    34         }
    35     }
    36 }
    37 
    38 int main()
    39 {
    40 
    41     char string[] = "abc";
    42     Permutation(string);
    43     
    44     return 0;
    45 }

  • 相关阅读:
    vue中router与route的区别
    网络编程-20200427
    JSP-20200428
    Servelet-1
    多线程-20200422-高级应用
    20200415_多线程——重点是应用场景
    2020-04-13注解和反射
    HIVE
    流的关联关系
    排序算法整理——坑+后期的数据结构部分
  • 原文地址:https://www.cnblogs.com/sankexin/p/5628235.html
Copyright © 2011-2022 走看看