用next_permutation()生成r-组合数,兼发现VC7的一个bug
2002-10-21 09:10
483 查看
C++ standard library提供了两个生成排列的algorithms:next_permutation()与prev_permutation(),却没有提供生成组合数的标准函数。
由于排列与组合之间有着密切的联系,我们很容易就可以从“排列”获得“组合”。从n个元素中任取r个元素的组合,有n! / (r! * (n-r)!)个。这些组合可用多重集{r·1, (n-r)·0}的全排列来生成,请看示例程序:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// 从n个元素中,任取r个元素的所有组合:
// G++[o] BCC5[o] VC7[x]
const int n = 7;
const int r = 4; // 0 <= r <= n
vector<int> p, set;
p.insert(p.end(), r, 1);
p.insert(p.end(), n - r, 0);
for ( int i = 0; i != p.size(); ++i )
set.push_back(i+1);
int count = 0;
do {
for ( int i = 0; i != p.size(); ++i )
if(p[i])
cout << set[i] << " ";
count++;
cout << "/n";
} while (prev_permutation( p.begin(), p.end() ));
cout << "There are " << count << " combinations in total.";
}
以上程序在G++ 3.2、BCC 5.5.1、VC7下编译通过,但在VC7版运行发生死循环。怀疑这是VC7的bug, 于是再用以下程序验证之:
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
// 经VC7编译执行后,会死循环,不可思议!
// BCC5[o] G++[o] VC7[x]
vector<int> p;
p.push_back(2);
p.push_back(2);
p.push_back(1);
do {
copy(p.begin(), p.end(), ostream_iterator<int>(cout, " "));
cout << "/n";
} while (prev_permutation(p.begin(), p.end()));
}
依旧是VC7版发生死循环,看来这真的是vc7中prev_permutation()的一个bug。我向http://www.dinkumware.com/提交了一份bug report,得到的回复是:
Our documentation points out, for both prev_permutation and
next_permutation that no two elements may have equivalent
ordering.
P.J. Plauger
经查,http://www.dinkumware.com/的文档中确实有这样的说法。而在MSDN中,我没有找到类似的提法,恐怕只能解释为MS的文档的更新太慢了。
由于排列与组合之间有着密切的联系,我们很容易就可以从“排列”获得“组合”。从n个元素中任取r个元素的组合,有n! / (r! * (n-r)!)个。这些组合可用多重集{r·1, (n-r)·0}的全排列来生成,请看示例程序:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// 从n个元素中,任取r个元素的所有组合:
// G++[o] BCC5[o] VC7[x]
const int n = 7;
const int r = 4; // 0 <= r <= n
vector<int> p, set;
p.insert(p.end(), r, 1);
p.insert(p.end(), n - r, 0);
for ( int i = 0; i != p.size(); ++i )
set.push_back(i+1);
int count = 0;
do {
for ( int i = 0; i != p.size(); ++i )
if(p[i])
cout << set[i] << " ";
count++;
cout << "/n";
} while (prev_permutation( p.begin(), p.end() ));
cout << "There are " << count << " combinations in total.";
}
以上程序在G++ 3.2、BCC 5.5.1、VC7下编译通过,但在VC7版运行发生死循环。怀疑这是VC7的bug, 于是再用以下程序验证之:
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
// 经VC7编译执行后,会死循环,不可思议!
// BCC5[o] G++[o] VC7[x]
vector<int> p;
p.push_back(2);
p.push_back(2);
p.push_back(1);
do {
copy(p.begin(), p.end(), ostream_iterator<int>(cout, " "));
cout << "/n";
} while (prev_permutation(p.begin(), p.end()));
}
依旧是VC7版发生死循环,看来这真的是vc7中prev_permutation()的一个bug。我向http://www.dinkumware.com/提交了一份bug report,得到的回复是:
Our documentation points out, for both prev_permutation and
next_permutation that no two elements may have equivalent
ordering.
P.J. Plauger
经查,http://www.dinkumware.com/的文档中确实有这样的说法。而在MSDN中,我没有找到类似的提法,恐怕只能解释为MS的文档的更新太慢了。
相关文章推荐
- 有红包给我啊!我不要两百,两块就行
- 进一步学习计算机的一些建议 (对非计算机专业)
- 计算机等级考试和C语言学习
- 不可忽视的界面设计
- D版带给我们的
- 《高质量C++编程指南》读书笔记(二)
- 《Modern C++ Design》源码Loki库读解随感二:类型间耦合检测和去耦合
- 任意一个字符串的全排列算法(可以有重复的字符)
- 在.net中读写INI文件
- 在RedHat Linux 7.3下成功安装Kylix 3.0
- 培养个人的计算机素质
- SaaS创业路线图(47):从管理会计看SaaS企业经营的真相(上)
- winsock 函数简介
- 如何学好VB
- WMI使用说明(翻译)
- 使用Log4j为项目配置日志输出应用详解以及示例演示的实现分析
- ADO 物件的方法與屬性
- 游戏开发者社区对底层API的投票
- 重锤痛击 robocode!
- 改善编码风格(超级简单实用)