2 u011832823 u011832823 于 2013.12.01 20:52 提问

关于斐波那契矩阵,用递归二分法。哪位大神帮忙看看哪里出问题了?

#include

#include

#include

#define MAX 100
using namespace std;

typedef struct fibo
{
int i1j1;
int i1j2;
int i2j1;
int i2j2;
}fibo;

fibo a[MAX];
fibo FIBO(int num);

fibo MUL(fibo x,fibo y)
{//x、y,2个矩阵相乘,
fibo w;
w.i1j1=x.i1j1*y.i1j1+x.i1j2*y.i2j1;
w.i1j2=x.i1j1*y.i1j2+x.i1j2*y.i2j2;
w.i2j1=x.i2j1*y.i1j1+x.i2j2*y.i2j1;
w.i2j2=x.i2j1*y.i1j2+x.i2j2*y.i2j2;
return w;

}
fibo FIBOMUL(fibo x,fibo y,fibo z,int num)
{
fibo r;
if(num<=2)
return((MUL(MUL(x,y),z)));
else
return(MUL(FIBO(num),FIBO(num)),z);

}
fibo FIBO(int num)
{
a[0].i1j1=a[0].i2j2=1; a[0].i1j2=a[0].i2j1=0;
a[1].i1j1=a[1].i1j2=a[1].i2j1=1; a[1].i2j2=0;
a[2].i1j1=2; a[2].i1j2=a[2].i2j1=a[2].i2j2=1;
fibo result=a[0];
if(num==1)
return a[1];
else if(num==2)
return a[2];
else{
if(num%2==1)
result=FIBOMUL(a[num/2],a[num/2],a[1],num/2);
else
result=FIBOMUL(a[num/2],a[num/2],a[0],num/2);
}
return result;
}

void main()
{
fibo x;
x=FIBO(8);
cout<<x.i1j1<<x.i1j2<<x.i2j1<<x.i2j2;
getchar();
getchar();

}

Csdn user default icon
上传中...
上传图片
插入图片