菜鸟写的c语,斐波那契数字
# include <stdio.h>int main( )
{
long int a[40],i=2,n;
for(;i<41;i++){
a[0]=a[1]=1;
a[i]=a[i-1]+a[i-2];
}
printf("请输入你需要的斐波那契数字数n(小于40):");
scanf("%d",&n);
printf("该数字为%d",a[n-1]);
}
大家看看出问题在哪?
#include <stdio.h> struct Matrix { int _11,_12; int _21,_22; }; void Ride(const Matrix *src1,const Matrix *src2,Matrix *out) { out->_11 = (src1->_11*src2->_11 + src1->_12*src2->_21)%10000; out->_12 = (src1->_11*src2->_12 + src1->_12*src2->_22)%10000; out->_21 = (src1->_21*src2->_11 + src1->_22*src2->_21)%10000; out->_22 = (src1->_21*src2->_12 + src1->_22*src2->_22)%10000; } void Power(Matrix *src,int n,Matrix *out) { Matrix one = {1,1,1,0}; if(n == 1) { *out = one; return ; } Power(src,n>>1,out); Matrix temp = *out; Ride(&temp,&temp,out); if(0 == n%2) return ; else { temp = *out; Ride(&temp,&one,out); } } int main() { int i,j,n; while(scanf("%d",&n) && n >=0) { if(0 == n || 1 == n) { printf("%d\n",n); continue; } Matrix m1 = {1,1,1,0},res; Power(&m1,n-1,&res); printf("%d\n",res._11); } return 0; }http://www. 对应题目