zoukankan      html  css  js  c++  java
  • 下一个排列/上一个排列-----全排列

    下一个排列 : next_permutation       只能从字典序的低向高 , 一个一个寻找出排列组合

    上一个排列 : prev_permutation   只能从字典序的高向低 , 一个一个寻找出排列组合

     1 //  上一个排列  -------  字典序从大到小
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 using namespace std;
    10 bool cmp(int a,int b)
    11 {
    12     return a>b;
    13 }
    14 int main()
    15 {
    16     int n,p[10];
    17     scanf("%d",&n);
    18     for(int i=0;i<n;i++)
    19         scanf("%d",&p[i]);
    20     sort(p,p+n,cmp);
    21     do
    22     {
    23         for(int i=0;i<n;i++)
    24             printf("%d ",p[i]);
    25         printf("
    ");
    26     }while(prev_permutation(p,p+n));
    27     return 0;
    28 }

    写法也就是这样下面附上 下一个排列---

     1 //  下一个排列  字典序 由小到大的排列方式
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 using namespace std;
    10 int main()
    11 {
    12     int n,p[10];
    13     scanf("%d",&n);
    14     for(int i=0;i<n;i++)
    15         scanf("%d",&p[i]);
    16     sort(p,p+n);
    17     do
    18     {
    19         for(int i=0;i<n;i++)
    20             printf("%d ",p[i]);
    21         printf("
    ");
    22     }while(next_permutation(p,p+n));
    23     return 0;
    24 }

  • 相关阅读:
    HTMLElement.dataset
    toLocaleString
    export,import ,export default是什么
    css鼠标禁用
    npm ERR! cb() never called! npm ERR! This is an error with npm itself.错误解决办法
    vue3的新特性
    el-dialog开启拖拽功能
    yarn config get registry
    JS中的函数防抖
    注入攻击的解决思路
  • 原文地址:https://www.cnblogs.com/A-FM/p/5308135.html
Copyright © 2011-2022 走看看