- UID
- 2198
注册时间2005-6-29
阅读权限255
最后登录1970-1-1
副坛主
该用户从未签到
|
// MyFei.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#define MAX 1000000000
#define LEN 1004
void BigNumAdd(int * num1,int * num2,int * len1,int * len2)
{
int temp = 0;
for(int i=0;i<*len2;i++)
{
num1[i] += num2[i] + temp; // += <==> +()
temp = num1[i]/MAX;
num1[i]%=MAX;
}
num1[i]+=temp;
if(*len1 < *len2)
{
*len1 = *len2;
}
if(num1[i])
(*len1)++; // 再次犯下同样的错误
}
void ShowBigNum(int * num1,int len1)
{
if(!num1[len1])len1--;
printf("%d",num1[len1--]);
for(int k=len1;k>=0;k--)
printf("%09d",num1[k]);
printf("\r\n");
}
int main(int argc, char* argv[])
{
int * num1 = new int[100];
int * num2 = new int[100];
memset(num1,0,sizeof(int)*100);
memset(num2,0,sizeof(int)*100);
int len1,len2;
len1 = len2 = num1[0] = num2[0] = 1;
for(int i=0;i<LEN;i++)
{
BigNumAdd(num1,num2,&len1,&len2);
BigNumAdd(num2,num1,&len2,&len1);
}
printf("斐波那契数列第%d项为:\r\n",LEN*2+1); // 单项+1 双项+2
ShowBigNum(num1,len1);
return 0;
}
斐波那契数列第2009项为:
32113249982681582845238472032097086207406078264369213828207269738873528372164658
13601607624368278410255618945484808988900205426234968094236646697230669809316249
08775724739402394881483009611433054415348267177640331222672835631629564907858584
30785050847615157489167011704773920617812432061755619769908742336741418110524930
07153851287942959207659531735086437334610990020876787656487609216126565715584580
25510084617009128909
请按任意键继续. . . |
|