#include <iostream>
#include <string>
using namespace std;
const int SIZE = 1001;
const int BASE = 10;
string result[SIZE];
string two("2");

void initial()
{
int mcarry, temp, carry;
int k;
string tempResult;
result[1] = "0";
result[2] = "1";
result[3] = "1";
result[4] = "3";
for (int i = 5; i < SIZE; ++i)
{
mcarry = 0;
for (int j = 0; j < two.length(); ++j) /*先做乘法,求出2^(n-3)*/
{
temp = 2 * (two[j] - '0') + mcarry;
mcarry = temp / BASE; /*乘法进位*/
temp = temp % BASE;
tempResult += (temp + '0');
}
if (mcarry != 0) /*进位不为0*/
{
tempResult += (mcarry + '0');
}

two = tempResult; /*存储计算结果*/
tempResult.clear();

int minLength = two.length() > result[i - 2].length() ? result[i - 2].length() : two.length();
carry = 0;

for (k = 0; k < minLength; ++k) /*然后做加法f(n) = f(n -2) + 2^(n - 3)*/
{
temp = (two[k] - '0') + (result[i - 2][k] - '0') + carry;
carry = temp / BASE; /*加法进位*/
temp = temp % BASE;
result[i] += (temp + '0');
}
/*两个数可能长短不一,所以要比较再相加*/
if (minLength < two.length())
{
for(; k < two.length(); k++)
{
temp = (two[k] - '0') + carry;
carry = temp / BASE;
temp = temp % BASE;
result[i] += (temp + '0');
}
}
if(minLength < result[i - 2].length())
{
for(; k < result[i - 2].length(); ++k)
{
temp = (result[i - 2][k] - '0') + carry;
carry = temp / BASE;
temp = temp % BASE;
result[i] += (temp + '0');
}
}
if (carry != 0) /*进位不为0*/
{
result[i] += (carry + '0');
}
tempResult.clear();
}
}
int main()
{
int n;
initial(); /*先打表*/
while (scanf ("%d", &n) != EOF)
{
for(int i = result[n].length(); i > 0; --i)
cout << result[n][i - 1]; /*这一步很关键,由于数据是倒着存的,所以要反着输出*/
cout << endl;
}
system ("pause");
return 0;
}

hdu1041的更多相关文章

  1. (大数)Computer Transformation hdu1041

    Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  2. hdu-1041(大数模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1041 题意:电脑中存在数字1,进行扩展操作,如果遇到1变为“01”,如果遇到0,变为“10”,经过一次 ...

  3. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

随机推荐

  1. 使用SQL Server 2005数据库管理工具 - 初学者系列 - 学习者系列文章

    本文讲述使用SQL Server 2005 Express数据库管理工具的使用. 1.打开数据库管理工具 2.选择下面的SQL Server 身份验证,因为在安装数据库的时候设置了sa的密码. 3.选 ...

  2. SVG 学习(一)

    SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. 什么是SVG? SVG 指可伸缩矢量图形 (Scalable Vector Gr ...

  3. 程序猿必要10免费的钱jquery小工具

    本周带来10款免费的jquery插件.假设你也有好的作品,欢迎分享到社区中来,在得到帮助的同一时候,也能与很多其它人分享来自你的作品. jQuery导航菜单置顶插件 - stickyUp . 在线演示 ...

  4. sqlserver查询所有表的行数的sql语句

    原文:sqlserver查询所有表的行数的sql语句 select object_name(id),rowcnt from sysindexes where indid<2 and object ...

  5. iis7 下配置MVC问题

    HTTP 错误 404.0 - Not Found 您要找的资源已被删除.已更名或暂时不可用. 详细错误信息 模块 IIS Web Core 通知 MapRequestHandler 处理程序 Sta ...

  6. beanutils通过SimpleProperty使用get或set方法赋值

    public class Employee { private String  firstName;    private String lastName;    public Employee() ...

  7. C语言中的内存对齐

    最近看了好多,也编了好多C语言的浩强哥书后的题,总觉的很不爽,真的真的好怀念linux驱动的代码,好怀念那下划线,那结构体,虽然自己还很菜. 同时看了一遍陈正冲老师的C语言深度剖析,收益很多,又把唐老 ...

  8. Roslyn and NRefactory

    1.Roslyn: 微软今天(2012-06-06)向CTP社区发布了Roslyn编译器的最新版本,它主要用于Visual Basic和C#代码,可工作于Visual Studio 2010 SP1和 ...

  9. VBS get,post函数

    Function gethttp(gethttp_url) Dim http_get Set http_get=Server.CreateObject("MSXML2.ServerXMLHT ...

  10. Hive UDAF开发之同时计算最大值与最小值

    卷首语 前一篇文章hive UDAF开发入门和运行过程详解(转)里面讲过UDAF的开发过程,其中说到如果要深入理解UDAF的执行,可以看看求平均值的UDF的源码 本人在看完源码后,也还是没能十分理解里 ...