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 ...
随机推荐
- springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)
参与该项目的文件上传和下载.一旦struts2下完成,今天springmvc再来一遍.发现springmvc特别好包,基本上不具备的几行代码即可完成,下面的代码贴: FileUpAndDown.jsp ...
- WebIM(5)----将WebIM嵌入到页面中
在之前的文章中,已经开发了一个简单的WebIM,但是这个WebIM是在独立的页面中的,今天发布的WebIM是一个可以嵌入到自己网页中的版本,你只需添加少量的代码,就可以在页面中嵌入一个WebIM.不过 ...
- 功能和形式的反思sql声明 一个
日前必须使用sql语句来查询数据库 但每次你不想写一个数据库中读取所以查了下反射 我想用反映一个实体的所有属性,然后,基于属性的查询和分配值 首先,须要一个实体类才干反射出数据库相应的字段, 可是開始 ...
- 实现透明渐变的Activity
如果光是透明全屏的Activity的话,直接继承内置theme即可 <activity android:theme="@android:style/Theme.NoTitleBar.F ...
- 基于.NET的微信SDK
超级懒汉编写的基于.NET的微信SDK 一.前言 特别不喜欢麻烦的一个人,最近碰到了微信开发.下载下来了一些其他人写的微信开发“框架”,但是被恶心到了,实现的太臃肿啦. 最不喜欢的就是把微信返回的 ...
- mvc3项目如何在IIS7.5上发布的
若项目拿到服务器上发布,必须要安装MVC3的安装包! 1.在vs中打开你要发布的项目,右键属性找到发布 2.弹出发布web对话框,选择<新建配置文件...> 在弹出的对话框中输入一个配置文 ...
- c#拷贝
话谈c#拷贝 c#中类型分为值类型和引用类型,值类型对象赋值是本身就是赋的自身的一个副本,而引用类型赋值时则赋的是指向堆上的内存,假如我们不想赋这个地址而想将对象赋过去要怎么做呢?首先要知道拷贝分为浅 ...
- 关于grub的那些事(一)
/etc/default/grub里的秘密: # If you change this file, run 'update-grub' afterwards to update # /boot/gru ...
- NotePad++安装和配置C/C++开发插件
NotePad++ - 安装和配置C/C++开发插件 | NotePad++ - Install and Configure plugins for develop C/C++ http://aofe ...
- AE基础知识之地图浏览
地图浏览:(放大缩小平移全图) //全局变量 public enum enumToolFlag { None ZoomOut, ZoomIn, Pan, } enumToolFlag flag = e ...