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. ArcGIS案例学习笔记-查找重叠的多边形

    ArcGIS案例学习笔记-查找重叠的多边形 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:对于多边形图层,查找具有重叠(相互覆盖)的面 数据: 方法: 1. ...

  2. Linux非常有用的命令

    <判断用户是否存在,如不存在则新建> user=`grep '^admin:' /etc/passwd`if [ -z "$user" ];then groupadd ...

  3. Promise 学习

    参考 https://www.jianshu.com/p/43f948051d65 // Promise里面传入一个函数类型的参数,这个函数类型的参数接收两个参数resolve reject var ...

  4. 云主机上配置lamp环境 php5.6+apache2.2.15+mysql5.1.73

    安装 PHP5.6 rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm; rp ...

  5. html页面嵌套---分享功能

    1.使用原因 项目中用到了thymeleaf去渲染模板,但是我们这有一个分享的页面,分享出去的页面要加上与生成模板不一样的内容.因为重新再加一套模板又会引起内容的重复与资源浪费.这里就用到了JS的lo ...

  6. js 浏览器页面切换事件

    document.addEventListener('visibilitychange', function() { console.log(isHidden() + "-" + ...

  7. webstocket 聊天

    /** * 初始化socket **/ function initSocket(index_host){//端口号 if( !window.WebSocket ){ console.log(" ...

  8. 03_java基础(二)之jdk的安装与环境变量配置

    1.语言与机器语言 语言 : 通常说的语言其实就是人与人之间沟通的一种方式计算机编程语言: 可以看成是人与计算机之间交流的一种方式 C,C++,C#,PHP,Java等 2.Java语言的历史 是SU ...

  9. support:design:26.1.0

    https://blog.csdn.net/qzltqdf3179103/article/details/79583491 compileSdkVersion 26buildToolsVersion ...

  10. 老代码:js实现二级城市联动(MVC)

    FormViewCity 为mvc控制器传给view的数据,包括一个MyCitys集合字段. <%@ Page Title="" Language="C#" ...