[HDU] [1715] Big Fibonacci Number

Problem Description

Loading...

Problem Analysis

Big Number arithmetic is simulated with strings.

In the Fibonacci sequence, this means adding Big Numbers repeatedly.
To make the function easier to write, the string type can be used.

Once the function is done, it only needs to be called the same way as a normal sequence lookup.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string F[1020]= {"0","1","2"};
string add(string a,string b) {
    a="0"+a;
    int la=a.length();
    int lb=b.length();
    for(int i=1; i<=la; i++) {
        if(lb-i>=0)
            a[la-i]+=b[lb-i]-'0';
        if(a[la-i]-'0'>9) {
            a[la-i]-=10;
            a[la-i-1]++;
        }
    }
    while(a[0]=='0')
        a.erase(0,1);
    return a;
}
int main() {
    for(int i=3; i<1002; i++) {
        F[i]=add(F[i-1],F[i-2]);
    }
    int T;
    scanf("%d",&T);
    while(T--) {
        char s[220];
        scanf("%s",&s);
        cout<<F[strlen(s)]<<endl;
    }
    return 0;
}

Loading...