#pragma once

#include <iostream>

using namespace std;

template <typename T> double *wMA(T &tArray, int iWMALen) // 应用模版数据类型 typename T 引用外部数组 tArray
{
int iArrayLen = sizeof(tArray) / sizeof(tArray[]); // 计算传入数组长度 = 总数组字节大小 / 首元素字节大小
cout << "Length of import array is: " << iArrayLen << endl;
cout << "Import array is:";
for(int i = ; i < iArrayLen; i++)
{
if(i == )
cout << "[" << tArray[i] << " ";
else if(i == iArrayLen - )
cout << tArray[i] << "]" << endl;
else
cout << tArray[i] << " ";
}
if(iArrayLen >= iWMALen && iWMALen > ) // 计算前提,数组长度不能小于均值要计算的天数
{
int *pWMAW = new int[iWMALen]; // 把费波纳茨数列个值做权重
int iSumW = ; // 费波纳茨数列的算术和
switch(iWMALen)
{
case :
{
cout << "Creating fabonacci numbers:" << endl;
pWMAW[] = ;
cout << "[0] = " << pWMAW[] << endl;
iSumW = pWMAW[];
break;
}
case :
{
cout << "Creating fabonacci numbers:" << endl;
pWMAW[] = ;
pWMAW[] = ;
cout << "[0] = " << pWMAW[] << endl;
cout << "[1] = " << pWMAW[] << endl;
iSumW = pWMAW[] + pWMAW[];
break;
}
default:
{
cout << "Creating fabonacci numbers:" << endl;
pWMAW[] = ;
pWMAW[] = ;
cout << "[0] = " << pWMAW[] << endl;
cout << "[1] = " << pWMAW[] << endl;
iSumW = pWMAW[] + pWMAW[];
for(int i = ; i < iWMALen; i++)
{
pWMAW[i] = pWMAW[i - ] + pWMAW[i - ]; // 费波纳茨通项公式算法
cout << "[" << i << "] = " << pWMAW[i] << endl;
iSumW += pWMAW[i];
}
break;
}
}
cout << "The sum of those fabonacci numbers is: " << iSumW << endl;
int n = iWMALen; // 固定均值计算天数,因为后面这个 iWMALen 会逐步增加变动
double *pWMAV = new double[iArrayLen]; // 用于保存均值
cout << "The wMA values are:" << endl;
while(iWMALen <= iArrayLen)
{
double iSumV = ;
for(int i = iWMALen - n, j = ; i < iWMALen; i++, j++)
{
iSumV += tArray[i] * pWMAW[j];
}
pWMAV[iWMALen - ] = iSumV / iSumW; // 算法 wMA = (数组个值 * 费波纳茨数列个值权重)的前 n 项和 / 费波纳茨数列加和
cout << "wMA[" << iWMALen - << "] = " << pWMAV[iWMALen - ] << endl;
iWMALen++;
}
delete(pWMAW); // 该指针作用到此为止,释放指针
cout << "Remeber to delete the return pointer of wMA(...)!\n" << endl; // 函数返回指针,提示后面删除堆中新建指针
return pWMAV;
}
else
{
cout << "Error!\n" << endl;
return NULL;
}
} int main()
{
int array[] = {, , , , , , , };
double *p = wMA(array, );
delete(p);
cin.get();
return ;
}

C++ 以费波纳茨数列为权重的加权均值计算方法 wMA的更多相关文章

  1. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  2. hdu 5459 Jesus Is Here (费波纳茨递推)

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission ...

  3. 分巧克力【来源:CSDN线上编程挑战赛】——递归,费波那奇数列,迭代

    /*====================================================================== 儿童节快到了,班长想要给班上的每个同学给一个巧克力, ...

  4. C语言 递归 汉诺塔问题 最大公约数问题

    函数不能嵌套定义,但能嵌套调用(在调用一个函数的过程中再调用另一个函数) 函数间接或直接调用自己,称为递归调用  汉诺塔问题 思想:简化为较为简单的问题 n=2 较为复杂的问题,采用数学归纳方法分析 ...

  5. 剑指Offer——滴滴笔试题+知识点总结

    剑指Offer--滴滴笔试题+知识点总结 情景回顾 时间:2016.9.18 15:00-17:00 地点:山东省网络环境智能计算技术重点实验室 事件:滴滴笔试   总体来说,滴滴笔试内容体量不算多, ...

  6. java程序设计课程实验报告1

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1353       姓名:陈都  学号:20135328 成绩:             指导 ...

  7. golang之数组

    1.数组:同一种数据类型的固定长度的序列. 2.数组定义:var a [len]int,例如:var a [5]int 3.长度是数组类型的一部分,因此,var a[5] int 和 var a[10 ...

  8. Javascript函数式编程要掌握的知识点讲解

    一:理解call和apply 及arguments.callee ECMAScript3给Function的原型定义了两个方法,他们是Function.prototype.call 和 Functio ...

  9. [No000019A]【波浪理论精典教学课程】

    波浪理论的产生和发展     拉尔夫·纳尔逊·艾略特(Ralph Nelson Elliott ),是波浪理论的创始人.1871年7月28日出生在美国密苏里州堪萨斯市的玛丽斯维利镇Marysville ...

随机推荐

  1. C++_直接插入排序

    #include <iostream> using namespace std; void insertSort(int a[], int n) {     for(int i=1;i&l ...

  2. 在EF4.1的DBContext中实现事务处理(BeginTransaction)和直接执行SQL语句的示例

    在EF4.1的DBContext中实现事务处理(BeginTransaction)和直接执行SQL语句的示例 (2012-03-13 10:12:48) 转载▼   public ActionResu ...

  3. centos 开启VNC

    安装软件包(有yum源安装的,不采用源码安装) yum -y install vnc vnc-server 安装成功后,配置如下: [root@localhost ~]# vncserver #设置 ...

  4. 【BZOJ-2937】建造酿酒厂 前缀和 + 展环为链 + 乱搞

    2937: [Poi2000]建造酿酒厂 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 70  Solved: 24[Submit][Status][D ...

  5. bzoj3514Codechef MARCH14 GERALD07加强版

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  6. ApsCMS AspCms_SettingFun.asp、AspCms-qqkfFun.asp、AspCms_Slide.asp、AspCms_StyleFun.asp、login.asp、AspCms_CommonFun.asp Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 AspCMS管理系统有较多漏洞,涉及到SQL注入.密码泄漏.后台写SHE ...

  7. QNetworkAccessManager 实现的 ftp 上传

    使用 QNetworkAccessManager 实现的 ftp 上传代码.完整可用,做个备忘. #include "mainwindow.h" #include <QDeb ...

  8. 控件的invoke和beginInvoke方法

    System.Windows.Forms.Timer 的timer是在主线程上执行的,因此在timer的tick事件中操作界面上的控件不会发生线程的安全性检测. Control的invoke和begi ...

  9. ubantu安装sogou输入法

    Ubuntu的搜狗输入法安装步骤   1 本来想先移除ibus,但是在之后发现如果直接使用下面的命令 sudo apt-get remove ibus 移除ibus将导致系统某些地方不正常的问题,例如 ...

  10. RabbitMQ安装配置

    安装RabbitMQ windows下的安装是非常简单的,我们需要准备两个东西 erlang的环境  下载windows和与之对象的操作系统位数安装包 http://www.erlang.org/do ...