Fibonacci Numbers

Description

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.

Input

One integer i

Output

Output the i-th Fibonacci number.

Sample Input

100

Sample Output

354224848179261915075

Hint

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

Code
import java.util.Scanner;
class Fibonacci{
    private String str;
    public Fibonacci(String str){
        this.str = str;
    }
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
}
public class Main {
    static Fibonacci [] fibonacci = new Fibonacci[50000];
    public static void main(String[] args) {
        createTable();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        System.out.println(fibonacci[n].getStr());
    }
    public static void createTable(){
        String x,y;
        x = "1";
        y = "1";
        fibonacci[1] = new Fibonacci("1");
        fibonacci[2] = new Fibonacci("1");
        for (int i = 3; i <= 5000; i++) {//4810
            String temp = calculate(x,y);
            x = y;
            y = temp;
            fibonacci[i] = new Fibonacci(temp);
            //System.out.println("fibonacci["+i+"]"+"="+fibonacci[i].getStr());
        }
    }
    public static String calculate(String str1,String str2){
        String [] temp = new String[50000];
        String [] t1 = str1.split("");
        String [] t2 = str2.split("");
        int i = t2.length - 1;
        int j = t1.length - 1;
        int y = 0, k = 0,s1=0,s2=0,sum;
        while(i>=0){
             s2 = Integer.valueOf(t2[i--]);
            if(j<0){
                s1 = 0;
            }else{
                s1 = Integer.valueOf(t1[j--]);
            }
            sum = (s2+s1+y)%10;
            temp[k++] = String.valueOf(sum);

            if(s1+s2+y>=10){
                y = 1;
            }else{
                y = 0;
            }
        }
        if(y==1){
            temp[k++] = "1";
        }
        StringBuffer sb = new StringBuffer();
        for (int l = k - 1; l >= 0; l--) {
            sb.append(temp[l]);
        }
        return sb.toString();
    }
}

#include<bits/stdc++.h>
using namespace std;
struct sumfibonacci{
	string str;
}strnum[5002];
string stradd(string str1,string str2)
{
	string temp;
	int k = 0;
	int slen1 = str1.size();
	int slen2 = str2.size();
	int i = slen2 - 1;
	int j = slen1 - 1;
	int y = 0,s1,s2,sum;
	while(i>=0){
		s2 = str2[i--] - '0';
		if(j<0){
			s1 = 0;
		}else{
			s1 = str1[j--] - '0';
		}
		sum = (s1 + s2 + y)%10;
		char csum = (sum + '0');
		temp += csum;
		if(s1 + s2 + y>=10){
			y = 1;
		}else{
			y = 0;
		}
		
	}
	if(y==1){
		temp += "1";
	}
	string tt;
	for(int k = temp.size()-1;k>=0;k--){
		tt +=temp[k];
	}
	return tt; 
}
void table()
{
	string x,y;
	x="1";
	y="1";
	strnum[1].str = x;
	strnum[2].str = y;
	for(int i=3;i<=5000;i++)
	{
		string temp = stradd(x,y);
		x = y;
		y = temp;
					
		strnum[i].str = temp;
	}
}
int main()
{
	int n;
	table();
	while(~scanf("%d",&n)){
		cout<<strnum[n].str<<endl;
    }
} 

C代码AC,Java代码提交WA,原因查找中。若看出问题,请指教!

Ps:Fibonacci [4810]项大于1000位

END
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值