HDU 1250 Hat's Fibonacci (递推、大数加法、string)

时间:2021-04-20 09:34:46

Hat's Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14776    Accepted Submission(s): 4923

 

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


Note:
No generated Fibonacci number in excess of digits will be in the test data, ie. F() = has digits.

问题大意与分析

就是一个变种的斐波那契,在处理大数相加的时候我用了string 被bug绊了好久...明天好好学学string

#include<bits/stdc++.h>

using namespace std;

int n,i;
string bigadd(string a,string b)
{
int jin=,i;
char ai,bi;
string anss=a;
int lena=a.size();
int lenb=b.size();
int lenmax=max(lena,lenb);
int p=lena-;
int q=lenb-;
for(i=lenmax-;i>=;i--)
{
if(p<)
ai='';
else
ai=a[p];
if(q<)
bi='';
else
bi=b[q];
anss[i]=((ai-''+bi-''+jin)%)+'';
jin=(ai-''+bi-''+jin)/;
p--;
q--;
}
if(jin)
{
char x=jin+'';
anss=x+anss;
}
return anss;
}
/*
int main()
{
while(scanf("%d",&n)!=EOF)
{
string a="1";
string b="1";
string c="1";
string d="1";
for(i=5;i<=n;i++)
{
string temp=d;
d=bigadd(bigadd(a,b),bigadd(c,d));
a=b;
b=c;
c=temp;
}
cout<<a <<b <<c <<d<<endl;
}
} */
int main(){
string a[]; //我之前用了4个string 出错了 好像是和长度有关
a[]="";
a[]="";
a[]="";
a[]="";
for(i=;i<;++i)
a[i]=bigadd(bigadd(bigadd(a[i-],a[i-]),a[i-]),a[i-]);
while(scanf("%d",&n)!=EOF)
{
cout<<a[n]<<endl;
}
return ;
}

HDU 1250 Hat's Fibonacci (递推、大数加法、string)的更多相关文章

  1. HDU 1250 Hat&&num;39&semi;s Fibonacci(大数相加)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...

  2. hdu 1250 Hat&&num;39&semi;s Fibonacci

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...

  3. HDOJ&sol;HDU 1250 Hat&&num;39&semi;s Fibonacci&lpar;大数~斐波拉契&rpar;

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  4. hdu 1250 Hat&&num;39&semi;s Fibonacci(java,简单,大数)

    题目 java做大数的题,真的是神器,来一道,秒一道~~~ import java.io.*; import java.util.*; import java.math.*; public class ...

  5. hdu 1250 Hat&&num;39&semi;s Fibonacci&lpar;高精度数&rpar;

    //  继续大数,哎.. Problem Description A Fibonacci sequence is calculated by adding the previous two membe ...

  6. HDU 1250 Hat&&num;39&semi;s Fibonacci(高精度)

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  7. POJ 2506 Tiling &lpar;递推 &plus; 大数加法模拟 &rpar;

    Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7965   Accepted: 3866 Descriptio ...

  8. hdu 4099 Revenge of Fibonacci 字典树&plus;大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  9. Children’s Queue HDU 1297 递推&plus;大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

随机推荐

  1. jqery筛选

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. PHP程序漏洞产生的原因和防范方法

    滥用include 1.漏洞原因: Include是编写PHP网站中最常用的函数,并且支持相对路径.有很多PHP脚本直接把某输入变量作为Include的参数,造成任意引用脚本.绝对路径泄露等漏洞.看以 ...

  3. 数据库备份工具mysqldump重要参数详解

    1. --single-transaction InnoDB 表在备份时,通常启用选项 --single-transaction 来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为:RE ...

  4. 将vs2012建的项目转换为vs2010项目

    vs2012建的项目vs2010打不开,但vs2010的项目vs2012能打开,所以我觉得vs2012没有对解决方案进行重大的调整,尝试修改了一下.sln文件,使用vs2010就能打开了,方法如下: ...

  5. hdu 5887 搜索&plus;剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. Python 生成requirement 使用requirements&period;txt安装类库

    快速生成requirement.txt的安装文件 (CenterDesigner) xinghe@xinghe:~/PycharmProjects/CenterDesigner$ pip freeze ...

  7. java学习笔记38(sql注入攻击及解决方法)

    上一篇我们写了jdbc工具类:JDBCUtils ,在这里我们使用该工具类来连接数据库, 在之前我们使用 Statement接口下的executeQuery(sql)方法来执行搜索语句,但是这个接口并 ...

  8. 32&period; linux下oracle数据库定时备份

    这里以oradatabak.sh(里面的内容要根据实际修改)脚本放在/u01/11g/datapump下为例: #1.添加脚本执行权限 chmod +x /u01/11g/datapump/orada ...

  9. ARM9&sol;ARM11&sol;Cortex A8处理器(转载) &period;

    //toppic:推荐几个值得中小企业使用的ARM9/ARM11/Cortex A8处理器 // 作者:gooogleman //原文地址:http://blog.csdn.net/goooglema ...

  10. centos infiniband网卡安装配置

    硬件:Mellanox InfiniBand,主要包括 HCA(主机通道适配器)和交换机两部分 软件:CentOS 6.4 MLNX_OFED_LINUX-2.1-1.0.0-rhel6.4-x86_ ...