-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.xml
More file actions
388 lines (371 loc) · 24.7 KB
/
atom.xml
File metadata and controls
388 lines (371 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://MineIdea.github.io</id>
<title>LT's Blog</title>
<updated>2020-04-03T08:54:15.307Z</updated>
<generator>https://github.com/jpmonette/feed</generator>
<link rel="alternate" href="https://MineIdea.github.io"/>
<link rel="self" href="https://MineIdea.github.io/atom.xml"/>
<subtitle>温故而知新</subtitle>
<logo>https://MineIdea.github.io/images/avatar.png</logo>
<icon>https://MineIdea.github.io/favicon.ico</icon>
<rights>All rights reserved 2020, LT's Blog</rights>
<entry>
<title type="html"><![CDATA[turtlebot与gazebo仿真问题]]></title>
<id>https://MineIdea.github.io/post/turtlebot-yu-gazebo-fang-zhen-wen-ti/</id>
<link href="https://MineIdea.github.io/post/turtlebot-yu-gazebo-fang-zhen-wen-ti/">
</link>
<updated>2020-04-03T08:52:42.000Z</updated>
<content type="html"><![CDATA[<p>学艺不精,使用turtlebot进行仿真时发现无论如何如何在gazebo都不能运行,我尝试使用mbot和turtlebot3进行gazebo仿真都很顺利,但这使用卡在我心上,在又学了一段时间ros后,我又回头来解决这个问题。</p>
<p>turtlebot不能在gazebo运行的关键原因就在于他的底盘是kobuki,因此我们会安装kobuki的相关资源,这些完成之后应该能操作现实的机器人,但要turtlebot与gazebo交互还差了一个模块:kobuki_gazebo</p>
<h3 id="查找控制插件">查找控制插件</h3>
<p>首先,我们要清楚ros和gazebo是独立的,ros和gazebo交互需要通过gazebo提供的plugin,在turtlebot和mbot中都有以下的plugin</p>
<pre><code> <!-- controller -->
<gazebo>
<plugin name="differential_drive_controller"
filename="libgazebo_ros_diff_drive.so">
<rosDebugLevel>WARN</rosDebugLevel>
<publishWheelTF>true</publishWheelTF>
<robotNamespace>/</robotNamespace>
<publishTf>1</publishTf>
<publishOdomTF>1</publishOdomTF>
<odometrySource>world</odometrySource> <!-- 'encoder' instead of 'world' is also possible -->
<publishWheelJointState>true</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>left_wheel_joint</leftJoint>
<rightJoint>right_wheel_joint</rightJoint>
<wheelSeparation>${wheel_joint_y*2}</wheelSeparation>
<wheelDiameter>${2*wheel_radius}</wheelDiameter>
<broadcastTF>1</broadcastTF>
<wheelTorque>30</wheelTorque>
<wheelAcceleration>1.8</wheelAcceleration>
<commandTopic>cmd_vel</commandTopic>
<odometryFrame>odom</odometryFrame>
<odometryTopic>odom</odometryTopic>
<robotBaseFrame>base_footprint</robotBaseFrame>
</plugin>
</code></pre>
<p>看到注释,我们也清楚这就是controller的功能,他所需要的库为libgazebo_ros_diff_drive.so,这是我们安装ros-full时就存在的,这插件也是控制ros的命令能够让gazebo反馈出来。</p>
<p>接下来我们分析turtlebot</p>
<p>在turtlebot_description/robots/kobuki_hexagons_kinect.urdf.xacro中</p>
<pre><code> <xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_common_library.urdf.xacro" />
<xacro:include filename="$(find kobuki_description)/urdf/kobuki.urdf.xacro" />
<xacro:include filename="$(find turtlebot_description)/urdf/stacks/hexagons.urdf.xacro"/>
<xacro:include filename="$(find turtlebot_description)/urdf/sensors/kinect.urdf.xacro"/>
</code></pre>
<p>引入了4个xacro文件,其中引入了kobuki.urdf.xacro</p>
<p>在kobuki_description/urdf/kobuki.urdf.xacro中引入了</p>
<pre><code> <xacro:include filename="$(find kobuki_description)/urdf/common_properties.urdf.xacro"/>
<xacro:include filename="$(find kobuki_description)/urdf/kobuki_gazebo.urdf.xacro"/>
</code></pre>
<p>其中kobuki_gazebo就是与gazebo交互的配置文件</p>
<p>我们找到他的controller</p>
<pre><code> <gazebo>
<plugin name="kobuki_controller" filename="libgazebo_ros_kobuki.so">
<publish_tf>1</publish_tf>
<left_wheel_joint_name>wheel_left_joint</left_wheel_joint_name>
<right_wheel_joint_name>wheel_right_joint</right_wheel_joint_name>
<wheel_separation>.230</wheel_separation>
<wheel_diameter>0.070</wheel_diameter>
<torque>1.0</torque>
<velocity_command_timeout>0.6</velocity_command_timeout>
<cliff_sensor_left_name>cliff_sensor_left</cliff_sensor_left_name>
<cliff_sensor_center_name>cliff_sensor_front</cliff_sensor_center_name>
<cliff_sensor_right_name>cliff_sensor_right</cliff_sensor_right_name>
<cliff_detection_threshold>0.04</cliff_detection_threshold>
<bumper_name>bumpers</bumper_name>
<imu_name>imu</imu_name>
</plugin>
</gazebo>
</code></pre>
<p>发现与turtlebot3不同,他的controller使用的插件为libgazebo_ros_robuki.so</p>
<h3 id="安装kobuki_gazebo插件">安装kobuki_gazebo插件</h3>
<p>找到kobuki_gazebo的plugin位置,我一开始以为是插件配置失败,又尝试了很多无用功。我突然想到定位这些库的位置,然后发现</p>
<pre><code>locate libgazebo_ros_kobuki.so
</code></pre>
<p>竟然为空!!!现在结果已经很明确了,因为缺少必要的库,所以无法正常控制turtlebot_gazebo</p>
<p>在搜索之后,我发现kobuki_gazebo的github为</p>
<pre><code>https://github.com/yujinrobot/kobuki_desktop
</code></pre>
<p>下载完并编译成功,我在自己的catkin_ws下找到了 libgazebo_ros_kobuki.so</p>
<p>至此</p>
<pre><code>roslaunch turtlebot_gazebo turtlebot_world.launch
rostopic pub -1 /mobile_base/commands/velocity geometry_msgs/Twist -- '[0.5,0,0]' '[0,0,0]'
</code></pre>
<p>已经可以让turtlebot在gazebo中运动起来</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[mbot添加bumper]]></title>
<id>https://MineIdea.github.io/post/mbot-tian-jia-bumper/</id>
<link href="https://MineIdea.github.io/post/mbot-tian-jia-bumper/">
</link>
<updated>2020-04-03T08:47:04.000Z</updated>
<content type="html"><![CDATA[<p>这里的mbot使用的是古月居课程中创建的mbot,本来也可以用雷达检测碰撞,但是延迟问题我没搞定,看到可以添加bumper来进行碰撞检测,没想到又是一个大坑。</p>
<p>我的问题是 添加了bumper后,即使机器人已经碰撞,但bumper并未输出碰撞信息,我在gazebo answer找到了相似的问题,但由于是Gazebo 7,而我的环境是Gazebo9,添加_collision已经不起作用,只能通过生成sdf来查看base_link的collison name。</p>
<p>以下为对我有帮助的网页连接:</p>
<p>[]: https://answers.ros.org/question/246448/getting-contact-sensorbumper-gazebo-plugin-to-work/<br>
[]: https://answers.gazebosim.org//question/15095/gazebo-contact-sensor-added-to-a-robot-link-in-urdf-file-is-not-working-as-expected/<br>
[]: https://blog.csdn.net/wubaobao1993/article/details/91890839</p>
<p>之后是我的整个操作流程。</p>
<p>操作都是在mbot_description/urdf/xacro/gazebo目录下进行</p>
<figure data-type="image" tabindex="1"><img src="https://MineIdea.github.io/post-images/1585903884200.jpg" alt="" loading="lazy"></figure>
<p>这里我创建一个mbot_with_bumper_gazebo.xacro来测试bumper能否正常使用。</p>
<p>全部代码为</p>
<pre><code><?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
<!-- bumper -->
<joint name="bumper_joint" type="fixed">
<axis xyz="0 1 0" />
<origin xyz="0.05 0 0.02" rpy="0 0 0"/>
<parent link="base_link"/>
<child link="bump_sensor"/>
</joint>
<link name="bump_sensor">
<collision name="bump_sensor_collision">
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.05 0.05 0.05"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.05 0.05 0.05"/>
</geometry>
</visual>
<inertial>
<mass value="1e-5" />
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="1e-6" ixy="0" ixz="0" iyy="1e-6" iyz="0" izz="1e-6" />
</inertial>
</link>
<gazebo reference="bump_sensor">
<sensor name="main_bumper" type="contact">
<contact>
<collision>base_footprint_fixed_joint_lump__base_link_collision</collision>
</contact>
<plugin name="gazebo_ros_bumper_controller" filename="libgazebo_ros_bumper.so">
<alwaysOn>true</alwaysOn>
<updateRate>50</updateRate>
<bumperTopicName>bumper_vals</bumperTopicName>
<frameName>world</frameName>
</plugin>
</sensor>
</gazebo>
<mbot_base_gazebo/>
</robot>
</code></pre>
<p>我们在mbot_base_gazebo.xacro中定义了基本的mbot,这里只需要导入并添加bumper即可。</p>
<p>但此时bumper并不一定能正常工作,原因就是</p>
<pre><code> <contact>
<collision>base_footprint_fixed_joint_lump__base_link_collision</collision>
</contact>
</code></pre>
<p>collison要关联机器人正确的名称,这里我已经通过生成sdf来找到正确名称了。</p>
<p>接下来是找到名称的步骤:</p>
<ol>
<li>rosrun xacro xacro.py -o robot.urdf robot.xacro //这里的robot.xacro为你的模型名称,在我们这里是mbot_with_bumper_gazebo.xacro</li>
<li>gz sdf -p robot.urdf > robot.sdf</li>
</ol>
<p>此时目录下已经有robot.sdf文件,我们打开在其中搜索base_link,因为一般机器人主体我们都用base_link来声明。</p>
<figure data-type="image" tabindex="2"><img src="https://MineIdea.github.io/post-images/1585903817019.jpg" alt="" loading="lazy"></figure>
<p>可以看到name是一个自动生成的名称,将name复制输入到collision当中</p>
<pre><code> <contact>
<collision>base_footprint_fixed_joint_lump__base_link_collision</collision>
</contact>
</code></pre>
<p>我们打开mbot launch文件</p>
<ol>
<li>rostopic pub -1 /cmd_vel geometry_msgs/Twist -- '[0.1,0,0]' '[0,0,0]' //让机器人运动起来</li>
<li>rostopic echo -n 1 bumper_vals</li>
</ol>
<p>我们已经可以看到有碰撞信息。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[turtlebot添加laser]]></title>
<id>https://MineIdea.github.io/post/turtlebot-tian-jia-laser/</id>
<link href="https://MineIdea.github.io/post/turtlebot-tian-jia-laser/">
</link>
<updated>2020-03-09T05:53:40.000Z</updated>
<content type="html"><![CDATA[<pre><code>系统信息: ros-melodic ubuntu18.04
</code></pre>
<p>在github上直接下载的turtlebot自带了kinect,并且可以使用depthimage_to_laserscan将深度信息转化为雷达信息,因此,如果添加雷达并不是必须的。我只是尝试增加雷达传感器来加深自己对机器人模型的理解。</p>
<p>1.保证你已经下载了必须的turtlebot包。</p>
<figure data-type="image" tabindex="1"><img src="https://MineIdea.github.io/post-images/1583733326591.jpg" alt="" loading="lazy"></figure>
<p>2.使用catkin_make确保已经编译,并source devel/setup.sh</p>
<p>如果编译出现问题,基本都是ros包未下载,根据错误安装对应的ros包。</p>
<pre><code>在bash中输入roslaunch turtlebot_gazebo turtlebot_world.launch
</code></pre>
<p><img src="https://MineIdea.github.io/post-images/1583733372465.jpg" alt="" loading="lazy"><img src="/home/lt/blog/turtlebot_laser/gazebo.jpg" alt="" loading="lazy"></p>
<p>可以打开gazebo并显示出turtlebot以及周围环境,如果有物品未加载</p>
<p>我在打开turtlebot_world的时候,红字报错提示nodelet failed,是因为turtlebot自带kinect,并默认启动depthimage_to_laserscan,因此要下载depthimage_to_laserscan在工作空间。</p>
<p>3.环境正常运行后,我们查看turtlebot_world.launch(如果不了解launch文件和xacro文件,去看一下介绍再继续进行)</p>
<p>这里对机器人进行加载的语句是</p>
<pre><code><include file="$(find turtlebot_gazebo)/launch/includes/$(arg base).launch.xml">
</code></pre>
<p>查看当前目录下的includes的kobuki.launch.xml,发现他使用变量3d_sensor指向了turtlebot/turtlebot_description/robots目录下的xacro文件。</p>
<p>因此,我们在该目录下创建机器人模型,命名为kobuki_hexagons_KinectHokuyo.urdf.xacro</p>
<pre><code><?xml version="1.0"?>
<!--
- Base : kobuki
- Stacks : hexagons
- 3d Sensor : kinect+hokuyo
-->
<robot name="turtlebot" xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_common_library.urdf.xacro" />
<xacro:include filename="$(find kobuki_description)/urdf/kobuki.urdf.xacro" />
<xacro:include filename="$(find turtlebot_description)/urdf/stacks/hexagons.urdf.xacro"/>
<xacro:include filename="$(find turtlebot_description)/urdf/sensors/kinect.urdf.xacro"/>
<xacro:include filename="$(find turtlebot_description)/urdf/sensors/hokuyo.urdf.xacro"/>
<kobuki/>
<stack_hexagons parent="base_link"/>
<sensor_kinect parent="base_link"/>
<sensor_hokuyo parent="base_link"/>
</robot>
</code></pre>
<p>注意,我的改动是在kobuki_hexagons_Kinect.urdf.xacro的基础上增加了一个laser</p>
<pre><code> <xacro:include filename="$(find turtlebot_description)/urdf/sensors/hokuyo.urdf.xacro"/>
</code></pre>
<p>之后,由于turtlebot并没有定义hokuyo,我们需要去路径turtlebot_description/urdf/sensors/下建立hokuyo.urdf.xacro urdf文件,配置位置信息。</p>
<pre><code><?xml version="1.0"?>
<!-- script_version=1.1 -->
<robot name="sensor_hokuyo" xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_gazebo.urdf.xacro"/>
<xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_properties.urdf.xacro"/>
<!-- RPLidar 2D LIDAR -->
<xacro:macro name="sensor_hokuyo" params="parent">
<joint name="laser" type="fixed">
<origin xyz="0.10 0 0.435" rpy="0 0.0 0.0" />
<parent link="base_link" />
<child link="hokuyo_link" />
</joint>
<!-- Hokuyo Laser -->
<link name="hokuyo_link">
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.1 0.1 0.1"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<mesh filename="package://turtlebot_description/meshes/sensors/hokuyo.dae"/>
</geometry>
</visual>
<inertial>
<mass value="1e-5" />
<origin xyz="0 0 0" rpy="0 0 0"/>
<inertia ixx="1e-6" ixy="0" ixz="0" iyy="1e-6" iyz="0" izz="1e-6" />
</inertial>
</link>
<!-- Set up laser gazebo details -->
<turtlebot_sim_2dsensor/>
</xacro:macro>
</robot>
</code></pre>
<p>代码中</p>
<pre><code><mesh filename="package://turtlebot_description/meshes/sensors/hokuyo.dae"/>
</code></pre>
<p>使用到了hokuyo.dae文件,这在我们预先下载的模型中可以找到。</p>
<pre><code>~/.gazebo/models/hokuyo/meshes/hokuyo.dae
</code></pre>
<p>将其复制到turtlebot_description/meshes/sensors/目录下。</p>
<p>4.在turtlebot_description/urdf/turtlebot_gazebo.urdf.xacro文件下添加配置laser在gazebo下的属性值插件配置,将以下代码插入文件中</p>
<pre><code><xacro:macro name="turtlebot_sim_2dsensor">
<gazebo reference="hokuyo_link">
<sensor type="ray" name="head_hokuyo_sensor">
<pose>0 0 0 0 0 0</pose>
<visualize>false</visualize>
<update_rate>40</update_rate>
<ray>
<scan>
<horizontal>
<samples>720</samples>
<resolution>1</resolution>
<min_angle>-1.570796</min_angle>
<max_angle>1.570796</max_angle>
</horizontal>
</scan>
<range>
<min>0.10</min>
<max>30.0</max>
<resolution>0.01</resolution>
</range>
<noise>
<type>gaussian</type>
<mean>0.0</mean>
<stddev>0.01</stddev>
</noise>
</ray>
<plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_laser.so">
<topicName>/turtlebot/laser/scan</topicName>
<frameName>hokuyo_link</frameName>
</plugin>
</sensor>
</gazebo>
</xacro:macro>
</code></pre>
<p>topicName标签定义了laser信息发布节点为/turtlebot/laser/scan</p>
<p>5.配置完成,由于launch文件根据env定义的变量来加载模型,我们需要在~/.bashrc中添加</p>
<pre><code>export TURTLEBOT_3D_SENSOR=KinectHokuyo
</code></pre>
<p>将加载的模型指向我们增加的机器人文件</p>
<p>6.运行</p>
<pre><code> roslaunch turtlebot_gazebo turtlebot_world.launch
// 查看topic
rostopic list
</code></pre>
<p>可以看到/turtlebot/laser/scan已经发布</p>
<pre><code>roslaunch turtlebot_rviz_launchers view_navigation.launch
</code></pre>
<p>将global options的Fixed Frame从map改为其他</p>
<p>把LaserScan订阅的节点改为/turtlebot/laser/scan,可以看到由深度图形转换的雷达信息没有本身雷达得到的信息丰富,也算是对的起我们的一番折腾了。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[剪绳子]]></title>
<id>https://MineIdea.github.io/post/cut_rope/</id>
<link href="https://MineIdea.github.io/post/cut_rope/">
</link>
<updated>2020-02-23T15:02:18.000Z</updated>
<content type="html"><![CDATA[<h2 id="题目描述">题目描述</h2>
<p>给你一根长度为n的绳子,请把绳子剪成整数长的m段(m、n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],...,k[m]。请问k[0]xk[1]x...xk[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。</p>
<h2 id="输入描述">输入描述:</h2>
<pre><code>输入一个数n,意义见题面。(2 <= n <= 60)
</code></pre>
<h2 id="输出描述">输出描述:</h2>
<pre><code>输出答案。
</code></pre>
<p>示例1</p>
<h2 id="输入">输入</h2>
<pre><code>8
</code></pre>
<h2 id="输出">输出</h2>
<pre><code>18
</code></pre>
<p>我看到题目的第一想法就是递归,首先考虑一下特殊情况,当n=1、2、3时,最大乘积分别为1,1,2<br>
n=4也是特殊情况,可以分成2*2,最大乘积为4。n>4时,将绳子分割一定大于本身长度,简单证明一下:</p>
<p class='katex-block'><span class="katex-display"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>2</mn><mo>∗</mo><mo>(</mo><mi>n</mi><mo>−</mo><mn>2</mn><mo>)</mo><mo>−</mo><mi>n</mi><mo>=</mo><mi>n</mi><mo>−</mo><mn>4</mn><mo>></mo><mn>0</mn><mo>(</mo><mi>n</mi><mo>></mo><mn>4</mn><mo>)</mo></mrow><annotation encoding="application/x-tex">2*(n-2) - n = n-4 > 0 (n>4)
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">∗</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">2</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68354em;vertical-align:-0.0391em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">0</span><span class="mopen">(</span><span class="mord mathdefault">n</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">4</span><span class="mclose">)</span></span></span></span></span></p>
<p>因此n>4之后都需要将绳子分成若干个2和3的长度,只需要比较是2 * cutRope(n-2)大,还是3 * cutRope(n-3)大。</p>
<p>但要注意一点,我们在绳子长度大于3时,要阻止程序对绳子长度为2、3进行进一步分割,使用Math.max就可以让绳子在长度为2、3时停止分割。</p>
<pre><code class="language-java">import java.lang.Math;
public class Solution {
public int cutRope(int target) {
if (target < 4) {
return target - 1;
}
return Math.max(2*Math.max(target-2,cutRope(target-2)),
3*Math.max(target-3,cutRope(target-3)));
}
}
</code></pre>
]]></content>
</entry>
</feed>