基于杨辉三角的组合数计算算法(C++实现)
关于组合数计算的算法我写了好几个,发现杨辉三角查表法效率比较高!
并且这个算法利用C(n,k) = C(n,n-k)来简化杨辉三角的数组,算法的时间复杂度比较低。
原创,水平有限,如有不足,欢迎指出。
Mail:rainday163@163.com
下面是我写的代码并附带测试函数:Dev-C++ 4.9.9.2下测试通过。
/*---------------------------------------------------------------------
//file name:combination.cpp
//Coder: rainday163
//E-mail: rainday1631@163.com
//Create date: 2009.9.4
//Last modify date: 2009.9.28
//Test platform: WinXP sp2 & Dev-C++ 4.9.9.2
---------------------------------------------------------------------*/
#include <typeinfo>
#include <vector>
#include <stdexcept>
#include <iostream>
template <typename T1,typename T2>
T1 combination(T2 const &n,T2 const &k)
{
//类型检查
if( !( (typeid(T1) == typeid(unsigned long long))
||(typeid(T1) == typeid(long long))
||(typeid(T1) == typeid(unsigned long))
||(typeid(T1) == typeid(long))
||(typeid(T1) == typeid(unsigned int))
||(typeid(T1) == typeid(int))
||(typeid(T1) == typeid(unsigned short))
||(typeid(T1) == typeid(short)) ) )
{
throw std::out_of_range("Type of return value Error!");
}
if( !( (typeid(T2) == typeid(long))
|| (typeid(T2) == typeid(unsigned int))
|| (typeid(T2) == typeid(int))
|| (typeid(T2) == typeid(unsigned short))
|| (typeid(T2) == typeid(short))
) )
{
throw std::out_of_range("Type of parameter Error!");
}
if((n|k)<0)//确保n,k为正值
{
throw std::out_of_range("Error:negative parameter!");
}
if(n<k)
{
throw std::out_of_range("Error:K > N ,Please check your input!");
&nb
相关文档:
CAPTION: 关于C/C++中内存空间的划分
AUTHOR: aIsland 摘自中国IT实验室
DATE: 2010-05-30
E-MAIL: aIsland@live.cn
QQ: 418662213
P.S.
1.Bolanlan|随心high|aIsland 三个网名均为本人
2.声明aIsland 所收录的所有文章其著作权都属于原创作者
  ......
1:SQL Server数据库配置
开启服务器
托盘显示服务器启动
2:在StaAfx.h 中添加如下代码
#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF","adoEOF")rena ......
1:在StaAfx.h 中添加如下代码 #import "C:\\Program Files\\Common Files \\System\\ado\\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
2:在BOOL CXXXApp::InitInstance()中初始化COM环境
::CoInitialize(NULL);
释放COM环境
::CoUninitialize();
......
Linux/UNIX C++高级培训---远程班
http://www.xuanyuan-soft.cn/index.php?option=com_content&view=article&id=84&Itemid=85
课程概要
培养目标
:Linux/UNIX C++软件工程师
专注Linux/UNIX服务器端的软件开发(后台开发),培养企业所需的专业Linux/UNIX C ......