您的位置:首页 > 其它

斐波那契数列

2014-02-07 15:07 113 查看
描述:请实现一个可以显示斐波那契列表的activity。

1. 请写一个仅有一个列表页面的Android应用

2. 列表从0开始,从小到大,每行显示一个斐波那契数字

列表的第n行显示F(n3),至少显示到 F(603)

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <time.h>
#include "fibo.h"
using namespace std;

static long fib(long a[], long b[]);

int _tmain(int argc, _TCHAR* argv[])
{
time_t time1,time2;
time(&time1);
//SYSTEMTIME time,time1;
//GetSystemTime(&time);

long a[5400]={0},o,x=1,temp[5400]={0} ,temp1[5400]={0} ,temp2[5400]={0},temp3[5400]={0};
temp2[5399]=1;
long l = 0;
for(long j=1; j<216001; j++){
/*for(long i=0; i<5400;i++){
temp[i] =temp2[i];
}*/

l=fib(temp1,temp2);

//for(long i=0; i<5400; i++){
//	temp1[i] = temp[i];
//}
/*for(long i=0; i<5400; i++){
temp2[i]=temp3[i];
}*/
if(j==x*x*x){
bool d = false;
for(long i=l; i<=5399; i++){
if(!d){
if(temp2[i]!=0)
d=true;
if(!d)
continue;
}
long t = temp2[i];
int w;
for(w = 0;t!=0;w++){
t = t/10;
}
w--;
cout<<(long)(temp2[i]/pow(10,w))<<"e"<<w+(5399-i)*9;
break;
}
cout<<endl;

cout<<x<<endl;
x++;
}

}
time(&time2);
cout<<time2-time1;
//GetSystemTime(&time1);
cin>>o;
return 0;
}

static long fib(long a[] ,long b[]){

long t;
static long l=0;
int s = 0;
for (long i = l; i <= 5399; i++)
{
t = b[i];
b[i] = a[i]+b[i];
a[i] = t;
}

for (long i = 5399; i >= 0; i--)
{
b[i - 1] += (long)(b[i] / 1000000000L);
b[i] = b[i] % 1000000000L;
if(b[i]==0){
s++;
if(s==5){
l = i;
break;
}
}
}

return l;
}
问题主要还是在防止溢出上面。本身应该用java解决的问题,而且java中有biginteger使用要方便得多。但是效率太低,尤其tostring(),耗时太久。最后还是选择了C++。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: