zoukankan      html  css  js  c++  java
  • 海涛老师的面试题作业28字符串的排列组合问题。

    View Code
    // 字符串的排列.cpp : 定义控制台应用程序的入口点。
    //
    
     /*************************************************
     设计者:cslave
     版本说明:本代码免费用于商用,拷贝,转移,使用,但是
     有本代码导致的问题,本人概不负责。
     设计时间:2012.6.30
     分发原则:遵守GNU规范。
     **************************************************/
    
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    void Swap(char& a,char& b)
    {
        char temp=a;
        a=b;
        b=temp;
    }
    
    
    void permutation(char* pStr,char* pBegin)
    {
        if(*pBegin=='\0')
            cout<<pStr<<endl;
        else
        {
    
            for(char *pCh=pBegin;*pCh!='\0';pCh++)
            {
                Swap(*pBegin,*pCh);
                permutation(pStr,pBegin+1);
                Swap(*pBegin,*pCh);
            }
        }
    }
    
    void permutation(char* pStr)
    {
        if(!pStr)
            return;
        else
            permutation(pStr,pStr);
    }
    
    
    
    void Combination(char* pStr,int Num,int Count ,char* arr)
    {
        if (Count==Num)
        {
            cout<<arr<<endl;
            return;
        }
        for(char* pTemp=pStr;*pTemp!='\0';pTemp++)
        {
            arr[Count++]=*pTemp;
            Combination(++pStr,Num,Count,arr);
            arr[--Count]='\0';
        }
    }
    
    void Combination(char* pStr)
    {
        if(!pStr)
            return;
        else
        {
            int Num=strlen(pStr);
            char arr[10];
            memset(arr,'\0',10);
            for(int i=1;i<=Num;i++)
            Combination(pStr,i,0,arr);
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char pStr[]="abcdefgh";
        Combination(pStr);
        permutation(pStr);
        return 0;
    }
  • 相关阅读:
    第二阶段Sprint2
    第二阶段Sprint1
    Sprint10
    Sprint9
    Sprint8
    Sprint7
    第二阶段个人工作总结(2)
    第二阶段个人工作总结(1)
    查找三个“水王”
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/cslave/p/2571282.html
Copyright © 2011-2022 走看看