Hat's Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12284    Accepted Submission(s): 4124

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 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.

用string会超时,以下为超时代码。

#include <iostream>
#include <string>
using namespace std;
string add(string a,string b)
{
int len1=a.length();
int len2=b.length();
int i;
if(len1>len2)
{
for(i=;i<=len1-len2;i++)
b=""+b;
}
else
{
for(i=;i<=len2-len1;i++)
a=""+a;
}
string str;
int cf=,t;
len1=a.length();
for(i=len1-;i>=;i--)
{
t=a[i]-''+b[i]-''+cf;
cf=t/;
t%=;
str=char(t+'')+str;
}
if(cf!=)
str=char(cf+'')+str;
return str;
}
string fun(int n)
{
string f[];
f[]="";
f[]="";
f[]="";
f[]="";
int i;
string a,b,c;
for(i=;i<=n;i++)
{
a=add(f[i-],f[i-]);
b=add(f[i-],f[i-]);
f[i]=add(a,b);
}
return f[n];
}
int main()
{
int n;
while(cin>>n)
{
string str;
str=fun(n);
cout<<str<<endl;
cout<<endl;
}
return ;
}

正确的代码

方法一:
利用二维数组和亿进制。
#include<cstdio>
#include <iostream>
#include<cstring>
using namespace std;
int str[][]; //必须为int类型。
int main()
{
memset(str,,sizeof(str));
str[][]=;
str[][]=;
str[][]=;
str[][]=;
int i,j,ans=,c,n;
for(i=;i<;i++)
{
for(j=,c=;j<;j++) //循环,来将j个数组的8位数字的情况全部列举出。
{
ans=str[i-][j]+str[i-][j]+str[i-][j]+str[i-][j]+c;
c=ans/;
str[i][j]=ans%; //每一个数组存8位数字,c来判定是否进位。
}
}
while(cin>>n)
{
j=;
while(!str[n][j]) //首位有0,清除掉0。
j--;
cout<<str[n][j]; //开头的首0清除掉后的x位数字,可能小于8位。
for(i=j-;i>=;i--)
printf("%08d",str[n][i]); //每8位数字输出一组,不足的自动部0。
printf("\n");
}
return ;
}
方法二:
利用滚动数组求解
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int t[][]; int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
memset(t,,sizeof(t));
t[][] = ;
t[][] = ;
t[][] = ;
t[][] = ;
for(int i = ;i < n;i++)
{
int carry = ;
int k = i % ;
for(int j = ;j < ;j++)
{
int x = t[][j] + t[][j] + t[][j] + t[][j];
t[k][j] = x + carry;
carry = t[k][j] / ;
t[k][j] %= ;
}
}
int k = ;
while(t[(n - ) % ][--k] == );
for(int i = k;i >= ;i--)
{
printf("%d",t[(n - ) % ][i]);
} printf("\n"); } return ;
}

用JAVA

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
BigInteger a[]=new BigInteger[10001];
int n;
while(in.hasNextInt()) {
n=in.nextInt();
a[1]=BigInteger.ONE;
a[2]=BigInteger.ONE;
a[3]=BigInteger.ONE;
a[4]=BigInteger.ONE;
for(int i=5;i<=10000;i++) {
a[i]=BigInteger.ZERO;
a[i]=a[i].add(a[i-1]);
a[i]=a[i].add(a[i-2]);
a[i]=a[i].add(a[i-3]);
a[i]=a[i].add(a[i-4]);
}
System.out.println(a[n]);
}
}
}

最新文章

  1. 2016 CCPC 东北地区重现赛
  2. Python 第一个Python项目Hello,Python 学习之路(二)
  3. 或得的一个div是变量时
  4. [ThinkPHP] 输出、模型的使用
  5. 膜拜acm大牛 虽然我不会这题,但是AC还是没有问题的~(转自hzwer)
  6. 用ActionSupport实现验证
  7. 在javascript中关于submit和button提交表单区别
  8. ubuntu下,thinkphp验证码不能用
  9. virtualenv 管理python 环境
  10. JS学习笔记——JavaScript继承的6种方法(原型链、借用构造函数、组合、原型式、寄生式、寄生组合式)
  11. 排序算法(Java实现)
  12. Jersey+mybatis实现web项目第一篇
  13. 十、Python练习----基础搭建飞机大战
  14. 高可用Redis(七):Redis持久化
  15. [spring boot] Table &#39;yhm.hibernate_sequence&#39; doesn&#39;t exist
  16. [No0000124]WPF 扩展控件Behavior的几种方式
  17. kafka的API操作
  18. java学习1ATM
  19. 【bzoj3172】 Tjoi2013—单词
  20. 扩展Spring切面

热门文章

  1. 回顾:前端模块化和AMD、CMD规范(全)
  2. db2安装
  3. c#词频统计命令行程序
  4. Daily Scrumming* 2015.12.18(Day 10)
  5. scipy的一些函数名
  6. Server Tomcat v7.0 Server at libra failed to start
  7. Oracle的一般监听问题解决
  8. [转帖]简析数据中心三大Overlay技术
  9. Node params和query的Get请求传参
  10. reshape、shuffle、save_weights