4

hdoj 1715 大菲波数

 3 years ago
source link: https://zxs.io/article/183
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

hdoj 1715 大菲波数

2013-09-06 分类:未分类 阅读(4303) 评论(0)

先java代码:

import java.util.Scanner;
import java.math.*;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		BigInteger fb[] = new BigInteger [1005];
		fb[1] = BigInteger.valueOf(1);
		fb[2] = BigInteger.valueOf(1);
		for (int i = 3; i < 1005; i++)
			fb[i] = fb[i-1].add(fb[i-2]);
		int t = cin.nextInt();
		while (t != 0) {
			t--;
			int n = cin.nextInt();
			System.out.println(fb[n]);
		}
		cin.close();
	}
}

然后是C++代码:

#include<stdio.h>
int fb[1001][100];
void add(int *s1,int *s2,int *s3)
{
    int t=0;
    for(int i=0;i<100;i++)
    {
        s3[i]=(s1[i]+s2[i])%10000+t;
        t=(s1[i]+s2[i])/10000;
    }
}
void print(int *s)
{
    for(int i=99;i>=0;i--)
        if(s[i]!=0)
            break;
    printf("%d",s[i--]);
    for(;i>=0;i--)
        printf("%04d",s[i]);
    puts("");
}
int main()
{
    int t,n;
    scanf("%d",&t);
    fb[1][0]=1;
    fb[2][0]=1;
    for(int i=3;i<=1000;i++)
        add(fb[i-1],fb[i-2],fb[i]);
    while(t--)
    {
        scanf("%d",&n);
        print(fb[n]);
    }
    return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK