C + + / Python description acwing 92. Recursive implementation of exponential enumeration

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


C++/python describe AcWing 92. Recursive implementation of exponential enumeration

Hello everyone , My name is Qi Guanjie (qí guān jié ), stay GitHub & CSDN Record the process of learning , Time flies , Future period , come on. ~ Bloggers are only in GitHub & CSDN Chinese blog , The only blog update address is : Qi Guanjie's blog , In the near future, we will gradually synchronize the relevant records to GitHub:Algorithmic-learning-records, Most of them are my records , If reprinted, please attach the original address , thank you .


Due to the need of study and work , Algorithm brush problem will gradually change from C++ towards Python3 excessive , In transition , If the implementation is not very beautiful , Please forgive me .

This article was originally written by Qi Guanjie , Please support the original , Some platforms have been stealing blog articles maliciously !!!
If you need to contact the blogger , You can contact me on wechat :qiguanjie2015


from 1∼n this n Randomly select any number of integers , Output all possible options .

Input format

Enter an integer n.

Output format

Output one solution per line .

The numbers in the same row must be in ascending order , Two adjacent numbers are used exactly 1 Space between .

For a scheme without any number of options , Output blank lines .

This question has a custom calibrator (SPJ), All walks of life ( Different options ) In any order .

Data range

1≤n≤151≤n≤15

sample input :

3

sample output :

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

Their thinking

dfs+ to flash back

The algorithm implements a C++

#include<iostream>
using namespace std;
const int N = 15;
int s[N],n;
void dfs(int u){

if(u == n){

// At the end of the search , Terminate and output 
for(int i = 0; i < n ; i++)
if(s[i] == 1)
cout << i+1 <<" ";
cout << endl;
return ;
}
s[u] = 2;
dfs(u+1);
s[u] = 1;
dfs(u+1);
// to flash back ,0 It's the first test ,1 Express choice ,2 Say no 
s[u] = 0;
}
int main(){

cin >> n;
dfs(0);
return 0;
}

Algorithm implementation 2 Python

n = int(input())
# 0 Represents the initial state ,1 Express choice ,2 Say no 
tmp = [0 for i in range(n)]
def dfs(u):
if u == n:
for i,d in enumerate(tmp):
if d == 1:
print(i+1,end=' ')
print('')
return None
tmp[u] = 2
dfs(u+1)
tmp[u] = 1
dfs(u+1)
tmp[u] = 0
dfs(0)
版权声明
本文为[Qi Guanjie]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/04/20210406142738282J.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学习教程!