关于全排列 next_permutation() 函数的用法

这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式。

1 int a[];
2 do{
3
4 }while(next_permutation(a,a+n));

下面的代码可产生1~n的全排列。

#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n)&&n){
        int a[1000];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        sort(a,a+n);//可以自行测试一下删除后的结果
        do{
            for(int i=0;i<n;i++)
                printf("%d ",a[i]);
            printf("\n");
        }while(next_permutation(a,a+n));
    }
    return 0;
}

例如输入

3

1 0 2

如果有sort()

输出为

0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0

若无

则输出为

1 0 2
1 2 0
2 0 1
2 1 0

可以发现少了许多种组合方法。

不过,仔细比较各种组合方法和有无sort()的输出,可以发现函数next_permutation()是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序。

时间: 2024-08-03 09:28:39

关于全排列 next_permutation() 函数的用法的相关文章

全排列 next_permutation() 函数的用法

在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: 1 #include <stdio.h> 2 #include <algorithm> 3 using namespace std; 4 int main(){ 5 int n; 6 while(scanf("%d",&n)&&n){ 7 int a[

全排列问题(next_permutation函数)

D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡出了个题目想难倒匡匡(小L很D吧),有一个数n(0<n<10),写出1到n的全排列,这时匡匡有点囧了,,,聪明的你能帮匡匡解围吗? 输入 第一行输入一个数N(0<N<10),表示有N组测试数据.后面的N行输入多组输入数据,每组输入数据都是一个整数x(0<x<10) 输出 按

next_permutation 函数

STL的next_permutation函数可以求出某个特定序列的下一个排列,当然,如果对一个给定序列,排序之后可以轻松求出全排列...... 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<map> 7 #include<set> 8 #includ

mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法

mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法: 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差.其结果的单位由interval 参数给出.interval 的法定值同TIMESTAMPADD()函数说明中所列出的相同. mysql> SELECT TIMESTAMPDIFF(MONTH,'200

【转】oracle的substr函数的用法

[转]oracle的substr函数的用法 oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串   substr( string, start_position, [ length ] ) 如:     substr('This is a test', 6, 2)     would return 'is'     substr('This is a test', 6)     would return 'is a test'     substr('TechOnThe

Oracle trunc()函数的用法

--Oracle trunc()函数的用法 /**************日期  TRUNC()函数没有秒的精确 ********************/ select sysdate from dual --当时日期 select trunc(sysdate) from dual select trunc(sysdate ,'DD') from dual --今天日期 select trunc(sysdate,'d')+7 from dual --本周星期日 select trunc(sys

C中的时间函数的用法

C中的时间函数的用法    这个类展示了C语言中的时间函数的常用的用法. 源代码: #include <ctime>#include <iostream> using namespace std; class MyTime{public:    MyTime() { mPTime = 0; mStLocalTime = 0; mStGMTTime = 0; }    ~MyTime() {}; //time_t time(time_t * timer) 返回自1970年1月1日00

嵌入式之---常用模板函数(用法说明函数、参数解析函数)

主要内容:嵌入式常用模板函数(用法说明函数.参数解析函数) /*显示参数列表*/ void usage() {     printf("usage: server [-p:x] [-i:IP] [-o]\n\n");     printf("       -p:x      Port number to listen on\n");     printf("       -i:str    Interface to listen on\n");

awk中split函数的用法

The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep. time=12:34:56 echo $time | awk '{split($0,a,":" ); print a[1]}' 12   echo $time | awk '{split($0,a,":" ); print a[3]}' 34   echo $time | awk