码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 排列(全排,前一个,下一个)


    目录

    一, 前一个排列(字典序更大的最近的一个排列)

     1.1 底层实现

          1.2  prev_permulation()

     二,下一个排列(字典序更小的最近的一个排列)

            1.1 底层实现 

         1.2   next_permulation()

    三,全排列

    四,元素想等的排列


     

    一, 前一个排列(字典序更大的最近的一个排列)

     1.1 底层实现

    1. #include
    2. using namespace std;
    3. const int maxn=103;
    4. int n,a[maxn];
    5. int main(){
    6. scanf("%d",&n);
    7. for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    8. int pos,sw;
    9. for(pos=n-1;pos>=1;--pos){
    10. if(a[pos]>a[pos+1])break;
    11. }
    12. for(sw=pos+1;sw<=n;++sw){
    13. if(a[sw]>a[pos])break;
    14. }--sw;
    15. // printf("%d %d !\n",pos,sw);
    16. swap(a[pos],a[sw]);
    17. sort(a+pos+1,a+n+1,greater<int>());
    18. for(int i=1;i<=n;++i)
    19. printf("%d ",a[i]);
    20. puts("");
    21. return 0;
    22. }

          1.2  prev_permulation()

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. int n;
    8. cin >> n;
    9. int a[105];
    10. for (int i = 0; i < n; i++)
    11. {
    12. cin >> a[i];
    13. }
    14. prev_permutation(a, a + n);
    15. for (int i = 0; i < n; i++)
    16. {
    17. cout<< a[i]<<" ";
    18. }
    19. return 0;
    20. }

     二,下一个排列(字典序更小的最近的一个排列)

            1.1 底层实现 

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int maxn = 103;
    7. int n, a[maxn];
    8. int main() {
    9. cin >> n;
    10. for (int i = 1; i <= n; ++i) cin>>a[i];
    11. int pos, sw;
    12. for (pos = n - 1; pos >= 1; --pos) {
    13. if (a[pos] < a[pos + 1])break;//与prev_permulation不同的地方 > 变 <
    14. }
    15. for (sw = pos + 1; sw <= n; ++sw) {
    16. if (a[sw] < a[pos])break;//与prev_permulation不同的地方 > 变 <
    17. }--sw;
    18. // printf("%d %d !\n",pos,sw);
    19. swap(a[pos], a[sw]);
    20. sort(a + pos + 1, a + n + 1, greater<int>());
    21. for (int i = 1; i <= n; ++i)
    22. printf("%d ", a[i]);
    23. puts("");
    24. return 0;
    25. }

         1.2   next_permulation()

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. int n;
    8. cin >> n;
    9. int a[105];
    10. for (int i = 0; i < n; i++)
    11. {
    12. cin >> a[i];
    13. }
    14. next_permutation(a, a + n);
    15. for (int i = 0; i < n; i++)
    16. {
    17. cout<< a[i]<<" ";
    18. }
    19. return 0;
    20. }

     

    三,全排列

            全排列没有现成的函数,我们可以利用 next_permulation() 获得。

    1.先将字符串从小到大排序

    2.用 do~while 循环,使用next_permultion() 不断获取下一个排列(字典序更小的排列)

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. int n;
    8. cin >> n;
    9. int a[105], b[105];
    10. for (int i = 0; i < n; i++)
    11. {
    12. cin >> a[i];
    13. }
    14. sort(a, a + n);
    15. do {
    16. for (int i = 0; i < n; i++)
    17. {
    18. cout << a[i] << " ";
    19. }
    20. cout << endl;
    21. } while (next_permutation(a, a + n));
    22. return 0;
    23. }

    四,元素想等的排列

             is_permutation(),只要元素相同就返回 true ,而不管顺序是否相同

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. int n;
    8. cin >> n;
    9. int a[105],b[105];
    10. for (int i = 0; i < n; i++)
    11. {
    12. cin >> a[i];
    13. b[i] = a[i];
    14. }
    15. next_permutation(a, a + n);
    16. cout << "a :" << endl;
    17. for (int i = 0; i < n; i++)
    18. {
    19. cout << a[i] << " ";
    20. }
    21. cout << endl;
    22. cout << "b :" << endl;
    23. for (int i = 0; i < n; i++)
    24. {
    25. cout << b[i] << " ";
    26. }
    27. cout << endl;
    28. if (is_permutation(a, a + n,b, b + n))
    29. {
    30. cout << "a 与 b 相等\n";
    31. }
    32. else
    33. {
    34. cout << "a 与 b 不相等\n";
    35. }
    36. return 0;
    37. }

     

  • 相关阅读:
    采用ROUANT 方法对 nex-gddp-cmip6 数据进行精度校正
    [kernel] 带着问题看源码 —— 进程 ID 是如何分配的
    java基础题
    java web 前置知识——Servlet(一)
    [附源码]Python计算机毕业设计爱行无忧旅游票务管理系统
    球形气膜:举办大型活动和特色景点的独特优势—轻空间
    [PowerQuery] PowerAutoMate 刷新PowerBI 数据
    java计算机毕业设计空闲教室查询系统MyBatis+系统+LW文档+源码+调试部署
    数据结构和算法(5):二叉树
    【C语言】文件相关函数详解
  • 原文地址:https://blog.csdn.net/weixin_62599885/article/details/127732544
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号