惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
C
Cisco Blogs
L
Lohrmann on Cybersecurity
宝玉的分享
宝玉的分享
I
Intezer
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
Cyberwarzone
Cyberwarzone
爱范儿
爱范儿
I
InfoQ
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
P
Palo Alto Networks Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
NISL@THU
NISL@THU
T
Threatpost
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Schneier on Security
Security Latest
Security Latest
Martin Fowler
Martin Fowler
H
Help Net Security
小众软件
小众软件
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
IT之家
IT之家
WordPress大学
WordPress大学
S
Securelist

博客园 - Zero Lee

调用栈(call stack) 关于STL allocator Calculate maximum sum of any subarray set Calcuate power n of x recursively Convert one binary search tree to double-linked list 设计包含min函数的栈 类模板的模板友元函数定义 一道百度的面试题解答 非printf形式的十六进制和二进制打印(雅虎面试题) 一道腾讯面试题 (转)C++中extern “C”含义深层探索 selection algorithm to select nth small elements based on partition 删除与某个字符相邻且相同的字符 产生全排列的方法解析 求一个正整数的平方根程序实现 [转]多线程队列的算法优化 [转载] STL allocator的介绍和一个基于malloc/free的allocator的简单实现 如何将一片内存链接成链表 One simple counted object pointer
一组数的全排列和组合程序实现
Zero Lee · 2012-06-17 · via 博客园 - Zero Lee

显示一组数的全排列和组合程序:

 1 void print(const std::vector<int>& s)
 2 {
 3     static int n = 1;
 4     printf("%d:", n++);
 5     printf("[");
 6     for (int i = 0; i < s.size(); i++)
 7         printf(" %d ", s[i]);
 8     printf("]\n");
 9 }
10 
11 void permutation(std::vector<int>& v, int beg, int end)
12 {
13     if (beg > end) {
14         print(v);
15         return;
16     }
17     for (int i = beg; i <= end; i++) {
18         std::swap(v[i], v[beg]);
19         permutation(v, beg+1, end); // pleate note, here always beg+1, not i+1
20         std::swap(v[i], v[beg]);
21     }
22 }
23 
24 void pm(std::vector<int>& v)
25 {
26     std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
27     printf("\nfull permulation are:\n");
28     permutation(v, 0, v.size()-1);
29 }
30 
31 void print(const std::vector<int>& v, int beg, int end)
32 {
33     static int n = 1;
34     printf("%d:", n++);
35     printf("[");
36     std::copy(v.begin()+beg, v.begin()+end+1, std::ostream_iterator<int>(std::cout, " "));
37     printf("]\n");
38 }
39 
40 void fullcombination(std::vector<int>& v)
41 {
42     printf("full combination are:\n");
43     for (unsigned int i = 0; i < v.size(); i++) {
44         print(v, i, i);
45         for (unsigned int j = i+1; j < v.size(); j++) {
46             for (int k = 0; k < v.size()-j; k++) {
47                 std::swap(v[j+k], v[j]);
48                 print(v, i, j);
49                 std::swap(v[j+k], v[j]);
50             }
51         }
52     }
53 }
54