fibonacci高精度加法

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

Input

Each line will contain an integers. Process to end of file.

Output

For each case, output the result in a line.

Sample Input

100

Sample Output

4203968145672990846840663646

Note:
No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <malloc.h>
using namespace std;

void add(char* a,char* b,char* c)
{
    int i,j,k,max,min,n,temp;
    char *s,*pmax,*pmin;
    max=strlen(a);
    min=strlen(b);
    if (max<min)
    {
        temp=max;
        max=min;
        min=temp;
        pmax=b;
        pmin=a;
    }
    else
    {
        pmax=a;
        pmin=b;
    }
    s=(char*)malloc(sizeof(char)*(max+1));
    s[0]=‘0‘;
    for (i=min-1,j=max-1,k=max; i>=0; i--,j--,k--)
        s[k]=pmin[i]-‘0‘+pmax[j];
    for (; j>=0; j--,k--)
        s[k]=pmax[j];
    for (i=max; i>=0; i--)
        if (s[i]>‘9‘)
        {
            s[i]-=10;
            s[i-1]++;
        }
    if (s[0]==‘0‘)
    {
        for (i=0; i<=max; i++)
            c[i-1]=s[i];
        c[i-1]=‘\0‘;
    }
    else
    {
        for (i=0; i<=max; i++)
            c[i]=s[i];
        c[i]=‘\0‘;
    }
    free(s);
}
char a[8001][2505];
int main(void)
{
    int i,n;
    for(i=1;i<=4;i++)
    strcpy(a[i],"1");
    for(i=5;i<8000;i++)
    {
        char x[2505],y[2505];
        add(a[i-1],a[i-2],x);
        add(a[i-3],a[i-4],y);
        add(x,y,a[i]);
    }
    while(cin>>n)
    {
        cout<<a[n]<<endl;
    }
    return 0;
}
时间: 2024-08-28 13:32:24

fibonacci高精度加法的相关文章

UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨论下怎么得到fib数的前40位. 首先,因为没可能每一项的fib都求出来的了.空间都存不下来.所以,只能够缩小规模,有个明显的道理,用每个fib的前60项相加取前40即可.为什么呢?因为没有后效性,后面的项相加不会影响到前40项.就是你有40--60这些项来缓冲就够了,这些才是主要的进位项.后面的相

问题 B: 【高精度】简单高精度加法

问题 B: [高精度]简单高精度加法 时间限制: 1 Sec  内存限制: 64 MB提交: 94  解决: 27[提交][状态][讨论版] 题目描述 修罗王解决了计算机的内存限制问题,终于可以使用电脑进行大型的魔法运算了,他交给邪狼的第一个任务是计算两个非负整数A.B的和,其中A和B的位数在5000位以内. 输入 共两行数据,第一行为一个非负整数A,第二行为一个非负整数B,A.B的位数均在5000以内. 输出 输出一个非负数,即两数之和. 样例输入 1111111111 2222222222

C语言(7)--高精度加法、减法、乘法、今天是星期几、四位平方数、候选人选票问题

1.高精度加法.减法.乘法 #include <stdio.h> #include <string.h> #include <malloc.h> void plus(char *a,char *b,char *c);//自定义高精度加法函数 void sub(char *a,char *b,char *c);//自定义高精度减法函数 void multiply(char *a,char *b,char *c);//自定义高精度乘法函数 int main() { char

中石油-【高精度】简单高精度加法

问题 B: [高精度]简单高精度加法 时间限制: 1 Sec  内存限制: 64 MB提交: 63  解决: 20[提交][状态][讨论版] 题目描述 修罗王解决了计算机的内存限制问题,终于可以使用电脑进行大型的魔法运算了,他交给邪狼的第一个任务是计算两个非负整数A.B的和,其中A和B的位数在5000位以内. 输入 共两行数据,第一行为一个非负整数A,第二行为一个非负整数B,A.B的位数均在5000以内. 输出 输出一个非负数,即两数之和. 样例输入 1111111111 2222222222

leetcode 66. Plus One(高精度加法)

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题解:简单的高精度加法,要注意[9]这种输入,要多一位. class Solution { public: vector<int> plusOne(v

【1002】高精度加法,此处应有掌声~(*/ω\*)

木有求助别人然后AC..... 打完了上面这一行我都羞愧(*/ω\*)谁叫我是蒟蒻呢 先上我滴最终代码 1 #include<stdio.h>//高精度加法 2 #include<string.h> 3 4 char ar[1001],br[1001]; 5 int a,b; 6 7 void exchange() 8 { 9 char cr[1001];int c; 10 strcpy(cr,ar); 11 strcpy(ar,br); 12 strcpy(br,cr); 13

A1087. 高精度加法

体验一下,高精度的算法 /* * ===================================================================================== * * Filename: a1087.c * * Description: Rainboy_RT * * Version: 1.0 * Created: 2014-07-04 14:54:08 * Revision: none * Compiler: gcc * * Author: Rain

用c++实现高精度加法

c++实习高精度加法 最近遇到一个c++实现高精度加法的问题,高精度问题往往十复杂但发现其中的规律后发现并没有那么复杂,这里我实现了一个整数的高精度加法,主要需要注意以下几点: 1:将所需输入的数据以字符数组的形式输入,建立字符数组,建立相应的整数数组,然后一一映射,以此来实现数据的输入,需要注意的是,当实现字符向数字映射时,应该减去相应的ASCII偏移值,即48. 2:为了模拟我们在纸上手算的进位模拟运算,我们将字符数组反向填入整数数组,上图的后几行代码实现了这个操作. 3:实现进位加法,这是

hdu 1250 Hat&#39;s Fibonacci(高精度数)

//  继续大数,哎.. Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your tas