斐波那契数列

描述:请实现一个可以显示斐波那契列表的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++。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值