易截截图软件、单文件、免安装、纯绿色、仅160KB

基于杨辉三角的组合数计算算法(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


相关文档:

C/C++中的日期和时间

摘要:
本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述。本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的详细使用方法。
关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间 ......

关于C/C++中内存空间的划分

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 所收录的所有文章其著作权都属于原创作者
   ......

[转]C/C++的64位整型

Technorati 标签: C++ 原文地址:http://www.byvoid.com/blog/c-int64/ 在C/C++中,64为整型一直是一种没有确定规范的数据类型。现今主流的编译器中,对64为整型的支持也是标准不一,形态各异。一般来说,64位整型的定义方式有long long和__int64两种(VC还支持_int64),而输出到标准输出方式有printf(“%lld”,a),printf ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号