加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

详谈全排列next_permutation() 函数的用法(推荐)

发布时间:2021-01-02 02:29:21 所属栏目:创业 来源:网络整理
导读:这是一个c++函数,包括在头文件algorithm内里,下面是根基名目。 1 int a[];2 do{3 4 }while(next_permutation(a,a+n)); 下面的代码可发生1~n的全分列 #include stdio.h#include algorithmusing namespace std;int main(){ int n; while(scanf("%d", for(int

这是一个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()是凭证字典序发生分列的,而且是从数组中当前的字典序开始依次增大直至到最大字典序。

以上这篇详谈全分列next_permutation() 函数的用法(保举)就是小编分享给各人的所有内容了,但愿能给各人一个参考,也但愿各人多多支持编程小能力。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读