【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)
本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的。
原题为hihocoder第77周的题目。
描述

Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral triangle, removing the inner third of each side, building another equilateral triangle at the location where the side was removed, and then repeating the process indefinitely.
Let Kn be the Koch Snowflake after n-th iteration. It is obvious that the number of sides of Kn, Nn, is 3*4n. Let's number the sides clockwisely from the top of Koch Snowflake.
Let si,n be the i-th side of Kn. The generation of si,n is defined as the smallest m satifying si,n is a part of the sides of Km. For example, in the above picture, the yellow sides are of generation 0; the blue sides are of generation 1; the red sides are of generation 2.
Given a side si,n, your task is to calculate its generation.
输入
The input contains several test cases.
The first line contains T(T <= 1000), the number of the test cases.
The following T lines each contain two numbers, i(1 <= i <= 10^9) and n(0 <= n <= 1000). Your task is to calculate the generation of side si,n.
输出
For each test case output the generation of the side.
- 样例输入
-
5
1 0
1 1
2 1
10 2
16 3 - 样例输出
-
0
0
1
2
0
题目大意:
给定一个三角形,每经过一次扩展,每一条边变成4条小边。告诉你扩展的次数n,以及边的编号i,求该条边是第几次扩展出现的边。
题目思路:
这道题初看是令人有点头疼的数学问题,需要找数字之间的规律,如果单看每一次扩展,找规律就会显得非常复杂,要让解题过程简单化,就应该从一条边看起。
我用hihocoder上题目分析的图片来解释会更加直观,例如第i条边经过扩展后:

也就是说中间两条边是经过第n次扩展后得到的,而左右两边则不能够确定。
左右两边经过规律的反推可以继续找这条件是否属于第n-1次扩展,否则继续减而治之。
具体代码如下:
//递归(减而治之)-单边分析
//第i条边经扩展后的四边编号
//i->4 * (i - 1) + 1, 4 * (i - 1) + 2, 4 * (i - 1) + 3, 4 * (i - 1) + 4
//Time:0Ms Memory:0K
#include<iostream>
#include<cstdio>
using namespace std; int cal(int side, int n)
{
if (n == )
return ;
if (side % == || side % == ) //如果边模4为2或3 则确定为n次扩展出的边
return n;
else //否则 减小一个规模continue
return cal((side + ) / , n - ); //降低的边数 经归纳可等价 (side + 3)/4
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int side, n;
scanf("%d%d", &side, &n);
printf("%d\n", cal(side, n));
} return ;
}
【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)的更多相关文章
- 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point
// 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- hiho一下 第207周
题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...
- hiho一下第128周 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- 【hiho一下】第一周 最长回文子串
题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...
- Solution: 最近公共祖先·一 [hiho一下 第十三周]
题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...
- 【hiho一下 第十周】后序遍历
[题目链接]:http://hihocoder.com/problemset/problem/1049 [题意] [题解] 前序遍历的第一个节点; 肯定是整颗树的头结点; 然后在中序遍历中; 得到这个 ...
- hiho一下十六周 RMQ-ST算法
RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当 ...
- hiho一下 第一百零七周 Give My Text Back(微软笔试题)
题目1 : Give My Text Back 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 To prepare for the English exam Littl ...
随机推荐
- 怎么搭建Web Api
1.通常我们有个web 让后可以有个web api 提供接口2.通常我们分别建两个项目 web api 依赖web的来进行访问(说到底是依赖是IIS)3.我们先建个SmallCode.Test.Web ...
- High Frequency Trading (整理中...)
什么是高频交易系统 1 交易指令完全是由电脑发送,对市场数据的响应延时在微秒级2 系统有专用的软硬件组成,研发时需要大量的计算机专家级的工作3 系统的硬件需要放在离交易所主机很近的位置,所谓co-lo ...
- R 字符串处理函数
用R来处理字符串数据并不是一个很好的选择,还是推荐使用Perl或者Python等语言.不过R本身也提供了一些常用的字符串处理函数,这篇文章就对这些字符串函数做一个简单的总结,具体各个函数的使用方法还是 ...
- Java反射机制(Reflection)
Java反射机制(Reflection) 一.反射机制是什么 Java反射机制是程序在运行过程中,对于任意一个类都能够知道这个类的所有属性和方法;对于任意一个对象都能够调用它的任意一个方法和属性,这种 ...
- python的re正则表达式模块学习
python中re模块的用法 Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
- 如何去各型MCU的官网上下载正确的数据手册
一.背景 感谢老司机左栋,虽然他一直很排斥这个名号 : ) ,可就技术上来说,还是当之无愧的. 弄了1年多单片机了,数据手册不是老员工或者头头给,就是从开发板资料拿.一直没有意识到,官网的东西才是最可 ...
- 71-IO 流
JAVA IO 流 一.概括 1.IO(INPUT OUTPUT)流 1.1 IO 流用来处理设备之间的数据传输 1.2JAVA 对数据的操作是通过流的方式 1.3 JAVA 用于操作流的对象都在 I ...
- 【血的教训】玩 Ubuntu 遇到的致命问题(进不了系统)及 解决方案
[问题1] 按照文章“U盘安装Windows 7 + Ubuntu 14 双系统笔记”在 Windows 7 基础上安装了 Ubuntu 14 系统,实现双系统切换,某一天, 通过如下命令行 sudo ...
- Ubuntu 12 修改当前用户密码:new password is too simple
修改当前登录用户的密码,通常使用如下命令: $ passwd Old password:****** New password:******* Re-enter new password:****** ...
- java之Timer
一.Java2的开发包中提供了一种很好使用的线程功能:你可以使用这些类创建后台进程,让其在等待一段规定的时间后执行,或者让其每隔一段时间执行.你也可以用Thread来完成,但利用Timer与Timer ...