1384 全排列

题目链接:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1384

给出一个字符串S(可能又重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:
 
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
Input
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
Output
输出S所包含的字符组成的所有排列
Input示例
1312
Output示例
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211

HINT:
题解:
一开始想多了,考虑了数字0开头的情况。。。
直接用stl里面的next_permuation这个就行了。水题啊。。
代码:
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000000+10;
string ans[maxn];
char num[9+10];

int main()
{
    while(scanf("%s",num)!=EOF)
    {
        int cnt=0;
        int l=strlen(num);
        for(int i=0;i<l;i++)
            if(num[i]=='0')
                cnt++;
        if(cnt==l)
        {
            for(int i=0;i<l;i++)
                printf("0");
            printf("
");
        }
        else
        {
            string s(num);
            sort(s.begin(),s.end());
            do
            {
                cout<<s<<endl;
            }while(next_permutation(s.begin(),s.end()));
        }
    }
}
原文地址:https://www.cnblogs.com/TAT1122/p/6024576.html