加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

PDSOJ 1048 Fibonacci Numbers(大数)

发布时间:2020-12-14 03:15:25 所属栏目:大数据 来源:网络整理
导读:Fibonacci Numbers 时间限制:? 1 Sec?? 内存限制:? 128 MB 提交:? 13?? 解决:? 2 [ 提交][ 状态][ 讨论版] 题目描述 A Fibonacci sequence is calculated by adding the previous two members of the sequence,with the first two members being both 1. f (

Fibonacci Numbers

时间限制:?1 Sec?? 内存限制:?128 MB
提交:?13?? 解决:?2
[ 提交][ 状态][ 讨论版]

题目描述

A Fibonacci sequence is calculated by adding the previous two members of the sequence,with
the first two members being both 1.
f (1) = 1,f (2) = 1,f (n > 2) = f (n ? 1) + f (n ? 2)
Your task is to take a number as input,and print that fibonacci number.

输入

100

输出

354224848179261915075

样例输入

100

样例输出

354224848179261915075

提示

No generated fibonacci number in excess of 1000 digits will be in the test data,i.e. f (20) = 6765

和大数加法类似,还要注意数据的变换s1,s2,sum之间的变换,sum=s1+s2.然后字符处理

#include<iostream>
#include<cstring>
using namespace std;
char sum[1200];
int s=0,m=0,n;
int main()
{
    cin>>n;
    string s1,s2;
    int a[10000],b[10000];
    int he,i;
    s1="1";
    s2="1";
    for(m=2;m<n;m++)
    {
        memset(a,sizeof(a));
        memset(b,sizeof(b));
        a[0]=s1.length();
        for(i=1;i<=a[0];i++)
        {
            a[i]=s1[a[0]-i]-'0';
        }
          b[0]=s2.length();
        for(i=1;i<=b[0];i++)
        {
            b[i]=s2[b[0]-i]-'0';
        }
        he=(a[0]>b[0]?a[0]:b[0]);
        for(i=1;i<=he;i++)
        {
            a[i]=a[i]+b[i];
            a[i+1]=a[i+1]+a[i]/10;
            a[i]=a[i]%10;
        }
        he++;
        while((a[he]==0)&&(he>1))
        he--;
        for(i=he,s=0;i>=1;i--,s++)
        {
            sum[s]=a[i]+'0';
        }
        s1=s2;
        s2=sum;
   }
        cout<<s2<<endl;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读