zoukankan      html  css  js  c++  java
  • 编程算法

    版权声明:本文为博主原创文章。未经博主同意不得转载。

    https://blog.csdn.net/u012515223/article/details/35593485

    字符串的排列 代码(C)


    本文地址: http://blog.csdn.net/caroline_wendy


    题目: 输入一个字符串, 打印出该字符串中字符的全部排列.


    方法: 使用递归依次交换位置, 打印输出.


    代码:

    /*
     * main.cpp
     *
     *  Created on: 2014.6.12
     *      Author: Spike
     */
    
    /*eclipse cdt, gcc 4.8.1*/
    
    #include <stdio.h>
    
    void Permutation(char* str, char* begin);
    
    void Permutation(char* str){
        if(str == NULL)
            return;
        Permutation(str, str);
    }
    
    void Permutation(char* str, char* begin) {
        if(*begin == '') {
            printf("%s
    ", str);
            return;
        }
    
    	for (char* c = begin; *c != ''; ++c) {
    		char temp = *c;
    		*c = *begin;
    		*begin = temp;
    
    		Permutation(str, begin + 1);
    
    		temp = *c;
    		*c = *begin;
    		*begin = temp;
    	}
    
    }
    
    void Test(char* pStr)
    {
        if(pStr == NULL)
            printf("Test for NULL begins:
    ");
        else
            printf("Test for %s begins:
    ", pStr);
    
        Permutation(pStr);
    
        printf("
    ");
    }
    
    int main(void)
    {
        char str[] = "abc";
        Test(str);
    
        return 0;
    }
    



    输出:

    Test for abc begins:
    abc
    acb
    bac
    bca
    cba
    cab
    




查看全文
  • 相关阅读:
    仿windows的嵌入式GUI系统(一)
    入群必看内容。
    嵌入式开发入门(2)
    仿windows消息机制的嵌入式GUI系统(二)(未完)
    使用硬件定时器软模拟多个定时器(8.15改进版)
    Python 文件目录操作
    C/C++ cast
    C/C++ basic
    C/C++ debug
    C++ 引用返回值
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10502289.html
  • Copyright © 2011-2022 走看看