2Q - Fibbonacci Number
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
//
| 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的更多相关文章
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
- HDU 2070 Fibbonacci Number
Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDUJ 2070 Fibbonacci Number
Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Fibbonacci Number(杭电2070)
/*Fibbonacci Number Problem Description Your objective for this question is to develop a program whi ...
- 杭电2070 Fibbonacci Number
Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdoj:2070
Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 杭电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 ...
- [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, ...
- HDU100题简要题解(2070~2079)
HDU2070 Fibbonacci Number 题目链接 Problem Description Your objective for this question is to develop a ...
随机推荐
- 面图层拓扑检查和错误自动修改—ArcGIS案例学习笔记
面图层拓扑检查和错误自动修改-ArcGIS案例学习笔记 联系方式:谢老师,135_4855_4328,xiexiaokui#139.com 数据源: gis_ex10\ex01\parcel.shp, ...
- Resize CentOS Linux hard drive partition (centos 6.3 调整LVS磁盘大小)
查看当前磁盘信息: [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用%% 挂载点/dev/mapper/VolGroup-lv_root ...
- UIApplication 的学习
1.0 URL 的组成 == 协议头://主机名/路径 从iOS7 开始,系统提供了两种管理状态栏的方式,默认交给控制器去管理 2.0 旋转事件----> UIApplication --- ...
- thymeleaf 的内置对象
- Java编译时多态和运行时多态
来源:https://blog.csdn.net/wendizhou/article/details/73733061 编译时多态:主要是方法的重载,通过参数列表的不同来区分不同的方法. 运行时多态: ...
- linux下的arm汇编程序
1.gnu 的编译环境搭建 解压编译工具,加入环境变量PATH 2.编译相关命令的使用 编译命令 arm-linux-gcc -g -c -o led.o main.o led.c main.c / ...
- 内存占用过高 kill 调整mysql内存占用
通过 /var/log/messages 查看 被系统kill掉的进程 如果是自己崩溃会产生 hs_err_ 修改mysql my.cnf innodb_buffer_pool_siz ...
- ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
原文链接:https://www.cnblogs.com/kaid/p/8616385.html 在ubuntu系统的termial下,用apt-get install 安装软件的时候,如果在未完成下 ...
- 游戏AI技术 2
[Unity3D人工智能编程精粹 2] 1.跟随领队行为. 用靠近(Seek)或追逐(Pursuit)实现跟随领队行为并不好.在Seek中,AI角色会被推向领队,最终与领队占据相同位置.而Pursui ...
- 最小齐套回写MO工单组件数量错误 SQL
SELECT * FROM OUT_MO_RES WHERE PEGGED_ID='001201271060'; --5000175080/160_1-MFG0011 SELECT * FROM V_ ...