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 ...
随机推荐
- 使用Flexible实现手淘H5页面的终端适配(转)
曾几何时为了兼容IE低版本浏览器而头痛,以为到Mobile时代可以跟这些麻烦说拜拜.可没想到到了移动时代,为了处理各终端的适配而乱了手脚.对于混迹各社区的偶,时常发现大家拿手机淘宝的H5页面做讨论—— ...
- ftoa浮法成字符串
#include <stdio.h> bool ftos(float num,char *s,int n) { int temp; float t=num; int pn=0; b ...
- C#边边角角(一)
前言 此篇为在学习C#基础时,熟悉C#的语法和高级特性的一些小的尝试和笔记,记录一下以供分享 集合初始化器 集合必须实现System.Collections.IEnumerable接口 集合必须包含A ...
- KMP算法简单回顾
前言 虽从事企业应用的设计与开发,闲暇之时,还是偶尔涉猎数学和算法的东西,本篇根据个人角度来写一点关于KMP串匹配的东西,一方面向伟人致敬,另一方面也是练练手,头脑风暴.我在自娱自乐,路过的朋友别太认 ...
- Javascript多线程引擎(九)
Javascript多线程引擎(九)--垃圾回收 垃圾回收这个话题对Programer来说是非常老旧的话题, 从手动的malloc/free 到半自动的 引用计数 再到全自动的 mark-sweep ...
- Visual Studio 2013 Use HTTPS (SSL) On Web Application Projects
公司调试HTTPS接口会用到,原文:http://www.codeproject.com/Tips/766918/Visual-Studio-Use-HTTPS-SSL-On-Web-Applicat ...
- C程序设计语言(第二版)习题:第二章
这一章习题做着很舒服,毕竟很简单.所以很有感觉. 练习 2-1 Write a program to determine the ranges of char , short , int , and ...
- linux的单用户模式
玩儿过linux的朋友,估计都有过遗忘超级用户密码或者把/etc/inittab./etc/rc.d/rc.sysinit之类文件误编辑,导致系统无法正常启动的恼人经历, 此类问题都可以通过单用户模式 ...
- T4 Template Overview
T4 Template Overview T4 Template的组成 指令区:为模板转换引擎提供指令,控制模板如何被处理 template:模板相关的属性,debug是否可以调试:hos ...
- 4 MySQL与PHP连接
目录: 1. 连接概述2. 创建php文件进行MySQL连接3. 查看连接效果 1. 连接概述 上文讲述了LAMP开发模型,并且使用AppServ进行安装.这时候就要体现优势了.本节将介绍在直接使用P ...