Your objective for this question is to develop a program which will generate a fibbonacci number. The fibbonacci function is defined as such: 
f(0) = 0 
f(1) = 1 
f(n) = f(n-1) + f(n-2) 
Your program should be able to handle values of n in the range 0 to 50. 

Input

Each test case consists of one integer n in a single line where 0≤n≤50. The input is terminated by -1.

Output

Print out the answer in a single line for each test case.

Sample Input

3
4
5
-1

Sample Output

2
3
5

Hint

you can use 64bit integer: __int64

// 递归
 #include<stdio.h>

 long fibbonacci(int n)
{
if(n==) return ;
else if(n==) return ;
else return fibbonacci(n-) + fibbonacci(n-);
} int main()
{
int n;
while(scanf("%d", &n), n!=-)
printf("%d\n", fibbonacci(n));
return ;
}

Time Limit Exceeded

// 
__intx
int/long __int64

signed: -2^31 ~ 2^31-1 ~ 2.1*10^9

unsigned:       0 ~ 2^32-1 ~ 4.29*10^9

signed:-2^63 ~ 2^63-1 ~ 9.2*10^18

unsigned:    0 ~ 2^64-1 ~ 1.8*10^19

// signed: scanf("%I64d",&a);    printf("%I64d",a);
  unsigned: scanf("%I64u",&a);    printf("%I64u",a);
// 说明:
  1、int64不能用作为循环变量
  2、int64的操作速度较慢
 #include<stdio.h>

 __int64 fibbonacci(int n)
{
__int64 x1=, x2=, x3=;
int i;
for(i=;i<=n;i++)
{
x3=x1+x2;
x1=x2; x2=x3;
}
if(x3) return x3;
else
{
if(n) return x2;
else return x1;
}
} int main()
{
int n; __int64 i;
while(scanf("%d", &n), n!=-)
{
i=fibbonacci(n);
printf("%I64d\n", i); /* __intxx io格式 */
}
return ;
}

AC

// 从第47个斐波那契数开始,其大小超过int范围;
// 从第48个斐波那契数开始,其大小超过unsigned int范围;
// 从第93个斐波那契数开始,其大小超过__int64范围;
// 从第94个斐波那契数开始,其大小超过unsigned __int64范围

2Q - Fibbonacci Number的更多相关文章

  1. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  2. HDU 2070 Fibbonacci Number

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDUJ 2070 Fibbonacci Number

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. Fibbonacci Number(杭电2070)

    /*Fibbonacci Number Problem Description Your objective for this question is to develop a program whi ...

  5. 杭电2070 Fibbonacci Number

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdoj:2070

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 杭电oj2031、2033、2070、2071、2075、2089、2090、2092、2096-2099

    2031  进制转换 #include<stdio.h> #include<string.h> int main(){ int n,i,r,x,j,flag; ]; while ...

  8. [Python] Advanced features

    Slicing 12345 L[:10:2] # [0, 2, 4, 6, 8]L[::5] # 所有数,每5个取一个# [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, ...

  9. HDU100题简要题解(2070~2079)

    HDU2070 Fibbonacci Number 题目链接 Problem Description Your objective for this question is to develop a ...

随机推荐

  1. Python类的进阶.md

    属性绑定 在python中可以给类对象动态的绑定属性 但是由于这种特性,随意动态绑定也会带来麻烦,因此可用__slots__来限制可绑定的属性名称 __slots__的绑定对于子类是不生效的,只对当前 ...

  2. PowerDesigner 修改table背景色

    Tools->Display Preferences(显示参数选择)->Format->Table->Modify->Fill->Fill color 出处:htt ...

  3. centos 7.4安装教程

    1. 2. 3. 4. 5. 6. 7. 8.

  4. 微信小程序 在canvas画布上划动,页面禁止滑动

    要实现微信小程序 在canvas画布上划动,页面禁止滑动,不仅要设置disable-scroll="true",还要要给canvas绑定一个触摸事件才能生效. <canvas ...

  5. pandas.DataFrame.to_excel

    1. file_name = 'aa.xlsx' df.to_excel(file_name) #这种file_name不能重复,已经生成过的就会报错 writer = pd.ExcelWriter( ...

  6. html开发环境

    标签(空格分隔): 环境 开发环境: 市面上有很多的HTML编辑器可以选择,常见的Hbuild.Sublime Text.Dreamweare都可以用来开发HTML. 当然PyCharm也支持HTML ...

  7. 【C++】正则表达式引擎学习心得

    最近参照一些资料实现了一个非常简易的正则表达式引擎,支持基本的正则语法 | + * ()等. 实现思路是最基本的:正则式->AST->NFA->DFA. 以下是具体步骤: 一. 正则 ...

  8. WPF HyperLink链接下划线隐藏

    两种方法: 1.在Grid标签内添加资源样式. <Grid.Resources> <Style TargetType="Hyperlink"> <Se ...

  9. php 读取网站页面源码的经典函数

    Snoopy.class.php下载 include "inc/Snoopy.class.php"; //读取网页,返回网页源文件内容 function read_url($str ...

  10. 今天设置apache二级域名ssl证书后出现问题

    用集成环境 phpwamp重启服务失败,但是重启电脑后apache就启动成功,也不知道哪里原因,待以后解决.