zoukankan      html  css  js  c++  java
  • 打印任意字符串排列组合

    #include <iostream>
    #include <string>

    using namespace std;

    void swap(string& s,int i,int j)
    {
    char a = s[i];
    s[i] = s[j];
    s[j] = a;
    }

    void myPrint(string& s, size_t index)
    {
    if (index >= s.size())
    {
    cout << s << endl;
    return;
    }

    for (size_t i = index; i < s.size(); i++)
    {
    swap(s ,index ,i);
    myPrint(s,index+1);
    swap(s, index, i);
    }
    }

    void test(string s)
    {
    myPrint(s, 0);
    cout << endl;
    }

    int main()
    {
    test("");
    test("a");
    test("ab");
    test("abc");
    test("abcd");
    return 0;
    }

  • 相关阅读:
    运算符与优先级
    数据类型
    c++基础
    有关进制
    函数二——递归
    字符串与随机数
    C语言第一课
    Linux-Shell
    Linux入门
    文本处理三剑客命令初探
  • 原文地址:https://www.cnblogs.com/itdef/p/6085926.html
Copyright © 2011-2022 走看看