关闭

print all permutation of a string(recursion)

标签: recursion
135人阅读 评论(0) 收藏 举报
分类:

Input a string, please print all permutation of it. For example, input”abs”,the output should be”abs”, “asb”, “bas”,”bsa”,”cas”,”csa”.

The recursion version:

#include<iostream>
using namespace std;
void CalAllPermutation(char *perm, int from, int to)
{
    if (to <= 1)
    {
        return;
    }
    if (from == to)
    {
        for (int i = 0; i <= to; i++)
        {
            cout << perm[i];
        }
        cout << endl;
    }
    else
    {
        for (int j = from; j <= to; j++)
        {
            std::swap(perm[j], perm[from]);
            CalAllPermutation(perm, from + 1, to);
            std::swap(perm[j], perm[from]);
        }
    }
}

int main()
{
    char arr[] = "abcde";
    CalAllPermutation(arr, 0, 4);
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:71164次
    • 积分:2934
    • 等级:
    • 排名:第12554名
    • 原创:215篇
    • 转载:15篇
    • 译文:2篇
    • 评论:16条
    最新评论