hdu1041
#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的更多相关文章
- (大数)Computer Transformation hdu1041
Computer Transformation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu-1041(大数模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1041 题意:电脑中存在数字1,进行扩展操作,如果遇到1变为“01”,如果遇到0,变为“10”,经过一次 ...
- ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...
随机推荐
- Wijmo 5 + Ionic Framework之:Hello World!
Wijmo 5 + Ionic Framework之:Hello World! 本教程中,我们用Wijmo 5 和 Ionic Framework实现一个Mobile的工程:Hello World. ...
- Plugin For KanColleViewer – Provissy Tools V1.0
これはKanColleViewerためのプラグインです,KanColleViewerの機能を拡張する. #介绍 / Introduction / 紹介这是一个KanColleViewer(俗称”提督很 ...
- Rich IntelliSense for jQuery
A while back we updated VS2008 IntelliSense to not fail when referencing jQuery. However, getting I ...
- location的使用
<script language="javascript" type="text/javascript"> function setUrl(){ ...
- Windows Store 应用
使用 Project Siena 生成一个 Windows Store 应用 继 App Studio 之后微软又一力作 Project Siena [Win8 应用神器]给初学开发 或 对 Wi ...
- The initialization of the CRM authentication pipline execution has failed
由于公司电路切换,昨天晚上不得不将服务器暂时关闭.早上重新开机时,发现开发环境连不上了.这可把我急坏了,大家可都等着开发呢. 于是查看服务器错误消息,发现时数据库连接连接不上. The initial ...
- C51编译器扩展的关键词 & C51中断函数的写法
C51根据单片机的特性扩展了相关的关键字,如下表示: 关键词 用途 说明 bit 位变量声明 声明了一个位变量或者位类型的函数 sbit 位变量声明 声明了一个可位寻址变量 sfr 特殊功能寄存器声明 ...
- jQuery的奥秘
颜海镜 高效jQuery的奥秘 讨论jQuery和javascript性能的文章并不罕见.然而,本文我计划总结一些速度方面的技巧和我本人的一些建议,来提升你的jQuery和javascript代码.好 ...
- 生成GUID字符串
//生成GUID字符串 string loginToken = System.Guid.NewGuid().ToString();
- windows服务1053错误排查
公司员工离职,接手他的任务,告诉我windows服务已经完成,没来得及测试.好吧,我接着做...... 服务生成后,运行bat文件.启动服务失败,提示错误编码1053.根据以往的经验,一般是三方面引起 ...