#include<iostream>
#include<stdlib.h>
using namespace std;
const int maxN = 60 ;
int N = 4 ,d = 2 , m = 3;
float a1 = 2 ,an = 5;
void input()
{
cout<<"input data N,d,a1,an,m"<<endl;
cin>>N>>d>>a1>>an>>m;
if( N > maxN )
{
cout<<"input error"<<endl;
return ;
}
if( N > m )
{
cout<<" m is too large "<<endl;
return ;
}
if( m < 0 )
{
cout<<"m must be a positive data."<<endl;
return;
}
}
//计算am与a1之间的关系s
//a[i] = s[m][0]*a[2] + s[i][1]*d + s[i][2]*a1
//a2 = ( an - s[N-1][1] * d - s[N-1][2] * a1 ) / s[N-1][0]
void calculate_s(float **s ,float *list)
{
s[0][0] = 0;
s[0][1] = 0;
s[0][2] = 1;
s[1][0] = 1;
s[1][1] = 0;
s[1][2] = 0;
for(int i = 2 ; i < N ; i++ )
{
s[i][0] = s[i-2][0] - 2 * s[i-1][0];
s[i][1] = s[i-2][1] - 2 * s[i-1][1] + 2;
s[i][2] = s[i-2][2] - 2 * s[i-1][2];
}
list[0] = a1;
list[1] = ( an - s[N-1][1] * d - s[N-1][2] * a1 ) / s[N-1][0] ;
for(int j = 2 ; j < N ; j++ )
{
list[j] = list[j-2] - 2*list[j-1] +2 * d;
}
}
//输出最后的结果
void out(float **s ,float *list)
{
int i;
for(i = 0 ;i < N ; i++ )
{
cout<<s[i][0]<<" "<<s[i][1]<<" "<<s[i][2]<<" "<<endl;
}
cout<<"data........"<<endl;
for(i = 0 ;i < N ; i++ )
{
cout<<list[i]<<" ";
}
cout<<endl;
}
int main()
{
//input();
//s[i][0]表示p s[i][1]表示q s[i][2]表示r
float **s = (float **)malloc(sizeof(float*) * N);
for(int ii = 0 ; ii < N ; ii++ )
s[ii] = (float *)malloc(sizeof(float) * 3);
float *list = (float *)malloc( sizeof(float) * N );
calculate_s(s , list );
out( s , list );
cout<<"\nthe result of "<<m<<" data is:";
cout<<list[m-1]<<endl;
return 0;
}
原文地址:http://blog.csdn.net/omenglishuixiang1234/article/details/45565321