zoukankan      html  css  js  c++  java
  • zoj 1828 Fibonacci Numbers

    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.


    Sample Input

    100


    Sample Output

    354224848179261915075


    Note:

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

    注意有多个测试样例。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    int n;
    
    int main() {
        while(~scanf("%d",&n)) {
            char s[2][1005] = {"1","1"};
            char *a = s[0],*b = s[1];
            for(int i = 3;i <= n;i ++) {
                int d = 0,j;
                for(j = 0;a[j];j ++) {
                    if(b[j]) d += b[j] - '0';
                    d += a[j] - '0';
                    b[j] = d % 10 + '0';
                    d /= 10;
                }
                while(d) {
                    b[j ++] = d % 10 + '0';
                    d /= 10;
                }
                b[j] = 0;
                swap(a,b);
            }
            reverse(a,a + strlen(a));
            printf("%s
    ",a);
        }
    }
  • 相关阅读:
    ElasticSearch入门到筋痛
    JavaScript学习(四)
    JavaScript学习(三)
    JavaScript学习(二)
    JavaWeb学习(一)
    final
    static
    object的方法
    java 数据类型
    spring mvc
  • 原文地址:https://www.cnblogs.com/8023spz/p/10433393.html
Copyright © 2011-2022 走看看