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. 在使用 #import <objc/message.h>时 xcode 报 :Too many arguments to function call, expected 0 , have * 解决方法

    选中项目 - Project - Build Settings - 

  2. WinForm textbox 全选

    原地址:忘了 textBox1.KeyPress += anyTextBox_KeyPress; private void anyTextBox_KeyPress(object sender, Sys ...

  3. EUREKA原理总结

    Eureka高可用架构 https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance 上图中主要的名称说明: Register:EurekaCli ...

  4. Windows 10 显示中的仅更改文本大小和加粗选项

    问题描述: 在Windows 10 1703 之前的版本,在控制面板-显示中,存在如下图中的图形界面设置: 系统升级到Windows 10 1703 或是Windows 10 1709 之后,不再存在 ...

  5. 输入框状态禁止enter键提交表单

    1:页面中如果存在input输入框和submit提交按钮时,默认按enter键会提交表单,如果我现在在做查询操作,一不小心按了enter键就会有提交表单的操作,这样显然是不合理的,所以我们要禁止按en ...

  6. css 鼠标选中内容背景色

    ::selection { background: rgba(32, 178, 170, .6); color: #ffffff; } ::-moz-selection { background: r ...

  7. 3D 模型

    http://www.imooc.com/article/12670

  8. 两种创建Observable的方法(转)

    转自:http://blog.csdn.net/nicolelili1/article/details/52038211 Observable.create() create()方法使开发者有能力从头 ...

  9. android显示和隐藏软键盘(转)

    显示键盘: EditText editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.request ...

  10. VHS介绍

    Java.JS HTTP流传输(VHS)介绍“我怎么才能让我的视频和Video.js一起玩?”“ 这是我们在使用Vio.js时最常见的问题之一.这是个好问题. 如果有人检查了Vo.js的拷贝,他们的内 ...