码迷,mamicode.com
首页 > 其他好文 > 详细

打印任意字符串排列组合

时间:2016-11-21 18:23:37      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:names   string   ++   space   ring   swap   字符串排列   size   turn   

 

#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;
}

 

打印任意字符串排列组合

标签:names   string   ++   space   ring   swap   字符串排列   size   turn   

原文地址:http://www.cnblogs.com/itdef/p/6085926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!