zoukankan      html  css  js  c++  java
  • next_permutation函数(全排列)

    题目描述:有一个数n(1<n<10),写出1到n的全排列。

    输入:第一行输入一个数n(0<n<10),表示有n组测试数据。后面的n行输入多组输入数据,每组输入数据都是一个整数x(0<x<10)

    输出按特定顺序输出所有组合。
    特定顺序:每一个组合中的值从小到大排列,组合之间按字典序排列。

    输入:

    2
    2
    3

    输出:

    12
    21
    123
    132
    213
    231
    312
    321
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[15];
    int main()
    {
        int n,x,i;
        cin>>n;
        ++n;
        while(--n)
        {
            cin>>x;
            for(i=0; i<x;++i)
                a[i]=i+1;
            for(i=0; i<x;++i)
                cout<<a[i];
            cout<<endl;
            while(next_permutation(a,a+x)==1)
            {
                for(i=0; i<x;i++)
                    cout<<a[i];
                cout<<endl;
            }
        }
        return 0;
    }

    函数next_permutation()是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序。

    next_permutation(),可以遍历全排列,要包含头文件<algorithm>

    与之完全相反的函数还有prev_permutation

    基本格式:

    int a[];
    while(next_permutation(a,a+n))
    {
    }
  • 相关阅读:
    技术学习沙龙
    mysql升级5.5
    mysql用户权限管理的问题
    dwz(jui)刷新当前dialog的方法
    perl进程管理一例
    cron执行service
    tp数据库表大写命名的一些问题
    php执行多个存储过程
    thinkphp使用中遇到的问题
    html5 ajax 文件上传
  • 原文地址:https://www.cnblogs.com/hrlsm/p/12833501.html
Copyright © 2011-2022 走看看