【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)

本文详细解析了Koch雪花曲线的生成算法,通过递归方式解决微软面试题,探讨了如何计算特定边在迭代过程中的生成代数。文章提供了具体代码实现,帮助读者理解并掌握这一经典的分形几何问题。

本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的。

原题为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次扩展,否则继续减而治之。

 

  具体代码如下:

  

 1 //递归(减而治之)-单边分析
 2 //第i条边经扩展后的四边编号
 3 //i->4 * (i - 1) + 1, 4 * (i - 1) + 2, 4 * (i - 1) + 3, 4 * (i - 1) + 4
 4 //Time:0Ms Memory:0K
 5 #include<iostream>
 6 #include<cstdio>
 7 using namespace std;
 8 
 9 int cal(int side, int n)
10 {
11     if (n == 0)
12         return 0;
13     if (side % 4 == 2 || side % 4 == 3)    //如果边模4为2或3 则确定为n次扩展出的边
14         return n;
15     else                                //否则 减小一个规模continue
16         return cal((side + 3) / 4, n - 1);    //降低的边数 经归纳可等价 (side + 3)/4
17 }
18 
19 int main()
20 {
21     int T;
22     scanf("%d", &T);
23     while (T--)
24     {
25         int side, n;
26         scanf("%d%d", &side, &n);
27         printf("%d\n", cal(side, n));
28     }
29 
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/Inkblots/p/5062341.html

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值