C + + / Python description acwing 94. Recursive implementation of permutation enumeration

Qi Guanjie 2021-04-06 14:31:04
python description acwing recursive implementation


C++/python describe AcWing 94. Recursively implement permutation enumeration

hold 1∼n this n Number of integers in a row and random order , Output all possible orders .

Input format

An integer n.

Output format

Output all solutions in order from small to large , Each row 1 individual .

First , Two adjacent numbers in the same line are separated by a space .

secondly , For two different lines , Compare the numbers of the corresponding subscripts one by one , Dictionary order is smaller in front .

Data range

1≤n≤9

sample input :

3

sample output :

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

The algorithm implements a C++

#include<iostream>
#include<algorithm>
using namespace std;
int main(){

int n,res[10] = {
0};
cin >> n ;
for(int i = 0 ; i < n; i++) res[i] = i+1;
do{

for(int i = 0 ; i < n ;i++)
cout<<res[i] <<" ";
cout<<endl;
}while(next_permutation(res,res+n));
return 0;
}

Algorithm implementation 2 C++

#include<iostream>
#include<algorithm>
using namespace std;
int main(){

int n,res[10] = {
0},m=1;
cin >> n ;
for(int i = 0; i < n; i++) res[i] = i+1;
for(int i = 1; i <= n; i++) m *= i;
for(int i = 0 ; i < n ;i++)
cout<<res[i] <<" ";
cout<<endl;
m--;
while(m--){

int i = n-1;
while(i && res[i] < res[i-1]) i--;
int j = i;
while(j + 1 < n && res[j+1] > res[i-1]) j++;
swap(res[i-1],res[j]);
reverse(res+i,res+n);
for(int i = 0 ; i < n ;i++)
cout<<res[i] <<" ";
cout<<endl;
}
return 0;
}

Algorithm implementation Three Python

from itertools import permutations
n = int(input())
res = [i+1 for i in range(n)]
for item in permutations(res):
for d in item:
print(d,end=' ')
print('')

Algorithm implementation four Python

from itertools import permutations
n = int(input())
res = [i+1 for i in range(n)]
m = 1
for i in range(n):
m *= (i+1)
for item in res:
print(item,end=' ')
print(' ')
m -= 1
while m:
i = n - 1
while n > 0 and res[i] < res[i-1]:
i -= 1
j = i
while j+1 < n and res[i-1] < res[j+1]:
j += 1
res[i-1],res[j] = res[j],res[i-1]
res[i:] = reversed(res[i:])
for item in res:
print(item,end=' ')
print('')
m -= 1
版权声明
本文为[Qi Guanjie]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/04/20210406142738276u.html

  1. Docker | 多图预警 | 配置Docker下Python开发环境
  2. Docker | multi graph warning | configuring Python development environment under docker
  3. 课程学习记录之python迭代器和生成器
  4. Python_学习之流程控制
  5. Python_学习之运算符
  6. Python_学习之基础数据类型
  7. Python_学习之虚拟环境的搭建
  8. Python iterator and generator of course learning record
  9. Python_ Process control of learning
  10. Python_ Learning operators
  11. Python_ Basic data types of learning
  12. Python_ The construction of virtual learning environment
  13. 一个非常简单好用的 Python 图形界面库
  14. 我就是这样学 Python 的
  15. 零基础,从一个抢票程序,提升自己的Python技能
  16. A very simple and easy to use Python graphical interface library
  17. That's how I learned python
  18. 商业数据分析从入门到入职(9)Python网络数据获取
  19. Zero basis, improve your Python skills from a ticket grabbing program
  20. 商业数据分析从入门到入职(8)Python模块、文件IO和面向对象
  21. 商业数据分析从入门到入职(7)Python基础数据结构及其操作
  22. 商业数据分析从入门到入职(6)Python程序结构和函数
  23. Business data analysis from entry to entry (9) Python Network Data Acquisition
  24. Business data analysis from entry to entry (8) Python module, file IO and object oriented
  25. Business data analysis from entry to entry (7) Python basic data structure and its operation
  26. Business data analysis from entry to entry (6) Python program structure and function
  27. 简简单单实现 Python Web 的登录注册页面,还包含一半逻辑。
  28. Simple implementation of Python web login registration page, but also contains half of the logic.
  29. 什么是pip?Python新手入门指南
  30. What is PIP? Getting started with Python
  31. Python uses for... Else to jump out of double nested loop
  32. Python基础之:Python中的内部对象
  33. 人工智能入门:Python实现机器学习
  34. The foundation of Python: inner objects in Python
  35. Introduction to artificial intelligence: machine learning in Python
  36. Python基础之:Python中的内部对象
  37. The foundation of Python: inner objects in Python
  38. Python 小技之 Office 文件转 PDF
  39. 还在为多张Excel汇总统计发愁?Python 秒处理真香!
  40. 用 Python 制作音乐聚合下载器
  41. Spark Delta Lake 0.4.0 发布,支持 Python API 和部分 SQL
  42. How to transfer office files to PDF
  43. Are you still worried about multiple excel summary statistics? Python second processing really fragrant!
  44. Making music aggregate downloader with Python
  45. Spark delta Lake 0.4.0 is released, supporting Python API and part of SQL
  46. Python信息搜集
  47. Python information gathering
  48. Python - 关于类(self/cls) 以及 多进程通讯的思考
  49. Python - thinking about class (self / CLS) and multi process communication
  50. Python - 关于类(self/cls) 以及 多进程通讯的思考
  51. Python - thinking about class (self / CLS) and multi process communication
  52. Python信用评分卡建模(附代码)
  53. Python credit score card modeling (with code)
  54. 学Python需要学数据库吗?Python学习教程!
  55. Do you need to learn database to learn Python!
  56. Python私有变量如何定义?Python学习教程!
  57. How to define Python private variables? Python tutorial!
  58. Python数据分析入门(六):Pandas的函数应用
  59. Introduction to Python data analysis (6): function application of pandas
  60. 学Python需要学数据库吗?Python学习教程!