poj2506 Tiling
http://poj.org/problem?id=2506
题目大意:用多少种方法可以用2*1或2*2瓦片来铺一个2*n的矩形?
这是一个2*17长方形的样品。

输入是一行行的序列,每一行包含一个整数0 <= n <= 250。对于每一行输入,在单独的行中输出一个整数,给出一个2 * n矩形的可能摆放方式数。
也就是说给一个2*n的棋盘,用三种方块(2*2,1*2,2*1)将其铺满,求有多少种可能性,通过给出的案例可以发现,输出的结果很大long long类型也存不下,所以要运用大整数的加法。作图可以发现递归方程式,对2 * 1,2 * 2,2 * 3来说 2 * 1有1种;2 * 2有3种;2 * 3 可以在2 * 2 的基础上加一个2 * 1竖着放的瓦片,在 2 * 1 的基础上加一个2 * 2的瓦片,也可以加两个2 * 1横着放的瓦片(竖着放与在 2 * 2的基础上重复)。num[3] = num[2]+ 2 * num[1]
算法思想:递归求解,这里采用一次性计算的迭代法提高算法的效率。我们可以发现递归的方程式:
1)num[i] = num[i - 1]+ 2 * num[i - 2]; i>2
2)num[0] = 1 ; i=0
3)num[1] = 1 ; i=1
4)num[2] = 3 ; i=2
#include <iostream>
#include <string>
using namespace std;
string Add(string str1, string str2)//大整数加法(两个正整数)
{
string str;
int len1 = str1.length();
int len2 = str2.length();
if (len1 < len2) //前面补0,使两个字符串长度相同
{
for (int i = ; i <= len2 - len1; i++)
str1 = "" + str1;
}
else
{
for (int i = ; i <= len1 - len2; i++)
str2 = "" + str2;
}
len1 = str1.length();
int cf = ;//进位
int temp;//当前位的值
for (int i = len1 - ; i >= ; i--)
{
temp = str1[i] - '' + str2[i] - '' + cf;
cf = temp / ;
temp %= ;
str = char(temp + '') + str;
}
if (cf != ) str = char(cf + '') + str;
return str;
}
int main()
{
string num[] = { "","","" };
for (int i = ; i <= ; i++)
{
num[i] = Add(num[i - ], Add(num[i - ], num[i-]));
}
int temp;
while (cin >> temp) {
cout << num[temp] << endl;
}
return ;
}
poj2506 Tiling的更多相关文章
- POJ2506——Tiling
Tiling Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a samp ...
- POJ2506:Tiling(递推+大数斐波那契)
http://poj.org/problem?id=2506 #include <iostream> #include <stdio.h> #include <strin ...
- Texture tiling and swizzling
Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...
- 图文详解Unity3D中Material的Tiling和Offset是怎么回事
图文详解Unity3D中Material的Tiling和Offset是怎么回事 Tiling和Offset概述 Tiling表示UV坐标的缩放倍数,Offset表示UV坐标的起始位置. 这样说当然是隔 ...
- POJ3420Quad Tiling(矩阵快速幂)
Quad Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3740 Accepted: 1684 Descripti ...
- Tri Tiling[HDU1143]
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏
Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8091 Accepted: 3918 Descriptio ...
- Tiling Up Blocks_DP
Description Michael The Kid receives an interesting game set from his grandparent as his birthday gi ...
- I - Tri Tiling
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status #in ...
随机推荐
- Java NIO系列教程(四) Scatter 和 Gather
Java NIO系列教程(四) Scatter 和 Gather Java NIO 开始支持 scatter/gather,scatter/gather 用于描述从 Channel(译者注:Chann ...
- Mockplus是如何节省你的原型时间的?
还在用老牌原型工具一点点绘制产品原型吗?还在为实现一个满意的交互而绞尽脑汁吗?还在为无法和用户高效沟通而发愁吗?朋友,现在是快速原型的时代了.时间不等人,当你精雕细琢完成产品启动页的时候,别人的原型已 ...
- RocketMQ的客户端连接数调查
RocketMQ版本:3.4.6 ==问题现象== RocketMQ集群的某个topic,在一部分节点上消费有“断层”,这部分数据一致没办法消费. ==调查过程== 一顿操作猛如虎的调查之后发现, 该 ...
- node.js初步总结
一:先上一段代码 process.argv.forEach(function (val, index, array) { console.log(index + ":" + ...
- SPSS—回归—曲线估计方程案例解析
上一节介绍了线性回归,虽然线性回归能够满足大部分的数据分析的要求,但是,线性回归并不是对所有的问题都适用, 因为有时候自变量和因变量是通过一个已知或未知的非线性函数关系相联系的,如果通过函数转换,将关 ...
- button设置边宽和圆角
UIButton *meifuButton = [UIButton buttonWithType:UIButtonTypeSystem]; [meifuButton setTit ...
- hdu3333 Turing Tree 2016-09-18 20:53 42人阅读 评论(0) 收藏
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Icicle partition
<!DOCTYPE html> <html> <head> <title>Icicle</title> <script type=&q ...
- EBS获取附件URL
http://wenku.baidu.com/link?url=MnYX269RBqW9ZRh-4famwduhYq9As0-vsIyVPA7aqv64cdxxjZEOaEE1_KZ9SGjY9qCx ...
- 使用 Project Siena 生成一个 Windows Store 应用
继 App Studio 之后微软又一力作 Project Siena [Win8 应用神器]给初学开发 或 对 Windows Store 应用感兴趣的同学们的一个福利,可以通过 一个简单的应用可以 ...