zoukankan      html  css  js  c++  java
  • 51Nod

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

    记录

    STL 生成字典序全排列函数 next_permutation(a,a+len)

    #include <algorithm>
    
    bool next_permutation( iterator start, iterator end );    //输出所有比当前排列大的排列,顺序是从小到大
    bool prev_permutation( iterator start, iterator end );    //输出所有比当前排列小的排列,顺序是从大到小

    AC代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <string>
    
    using namespace std;
    string s;
    const int maxn = 20;
    int a[maxn];
    
    int main()
    {
        while( cin >> s )
        {
            int len = s.length();
            for( int i = 0 ; i < len ; i++ )
                a[i] = s[i] - '0';
            sort(a,a+len);    //先排序为字典序最小的情况
            do
            {
                for( int i = 0; i < len; i++ )
                    cout << a[i];
                puts("");
            }
            while( next_permutation(a,a+len) );
        }
        return 0;
    }
  • 相关阅读:
    IO多路复用
    事件驱动模型
    协程
    进程
    py2与py3的编码问题
    Linux Centos7 网卡无法启动
    监控的法则
    如何优雅的采集activeMQ性能指标
    一分钟性能分析
    beta版 tomcat 应用监控指标
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740651.html
Copyright © 2011-2022 走看看