当前位置:网站首页>Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
2022-07-01 12:40:00 【51CTO】
List of articles
- Question
- Ideas
- Code
Question
Title Description
You have a balance . Now you have to design a set of weights , So that any weight less than or equal to... Can be weighed by using these weights
N
Positive integer weight of . How many weights should this set of weights contain at least ?
Note that the weight can be placed on both sides of the balance .
Input
The input contains a positive integer
N.
Output
Output an integer to represent the answer .
The sample input
7
Sample output
3
Tips
【 Sample explanation 】
3
The weight of a weight is
1
、4、6, It can be said that
1
to
7
All the weight of .
1
=
1
;
2
=
6
4 (
Put the balance aside
6
, On the other side
4)
;
3
=
4
1
;
4
=
4
;
5
=
6
1
;
6
=
6
;
7
=
1
+
6
;
Less than
3
A weight cannot weigh
1
to
7
All the weight of .
【 Evaluate use case size and conventions 】
For all profiling use cases ,1
≤
N
≤
1000000000
.
- 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.
Ideas
At first, I thought of two points
Later, I found that I didn't know how to judge whether I could express 1-n Number of numbers
Start typing your watch to find the rules Find the first i The maximum number that can be represented by a weight is i-1 Three times the maximum number that a weight can represent +1 Recursion
Code
def
f(
n):
'''
n>=2 f(n) The number of representative weights is n The maximum number that can be represented when f(1) = 1 # Recurrence
call 1 need 1 A weight Weight of weight 1
call 2 need 2 A weight Weight of weight 1 2
call 3 need 2 A weight Weight of weight 1 2
call 4 need 2 A weight Weight of weight 1 3
call 5 need 3 A weight Weight of weight 1 2 3
call 6 need 3 A weight Weight of weight 1 2 3
call 7 need 3 A weight Weight of weight 1 3 6
call 8 need 3 A weight Weight of weight 1 5 7
call 9 need 3 A weight Weight of weight 1 3 8
call 10 need 3 A weight Weight of weight 1 3 8
call 11 need 3 A weight Weight of weight 1 3 8
call 12 need 3 A weight Weight of weight 1 3 8
call 13 need 3 A weight Weight of weight 1 3 9
'''
if
n
==
1:
return
1
return
f(
n
-
1)
*
3
+
1
while
True:
try:
n
=
int(
input())
res
=
1
while
f(
res)
<
n:
res
+=
1
print(
res)
except:
break
- 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.
边栏推荐
- 【MAUI】为 Label、Image 等控件添加点击事件
- Chapter 14 signals (IV) - examples of multi process tasks
- ROS2 Foxy depthai_ros教程
- What are the PHP FPM configuration parameters
- 手把手教你完成图像分类实战——基于卷积神经网络的图像识别
- 【20211129】Jupyter Notebook遠程服務器配置
- The difference between memcpy and strcpy
- I wish you all a happy reunion
- GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
- STM32 project practice (1) introduction and use of photosensitive resistor
猜你喜欢

Chapter 14 signals (IV) - examples of multi process tasks

79. 单词搜索【dfs + 回溯visit + 遍历起点】

leetcode:226. 翻转二叉树【dfs翻转】
![[encounter Django] - (II) database configuration](/img/13/9512c1e03349092874055771c3433d.png)
[encounter Django] - (II) database configuration

晓看天色暮看云,美图欣赏

Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable

I wish you all a happy reunion

A hole in solder paste

Double linked list related operations
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
随机推荐
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
Ipv6-6to4 experiment
Need your own cognition
Huawei interview question: Recruitment
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
阿霍的三个阶段
79. Word search [DFS + backtracking visit + traversal starting point]
微信模拟地理位置_伪装微信地理位置
kubernetes之ingress探索实践
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
Scene function of wooden frame
ASP. Net core 6 from entry to enterprise level practical development application technology summary
Double linked list related operations
redis探索之缓存一致性
MySQL data table creation
Eurake partition understanding
Topic 1004: the story of cows (recursion)
QT 播放器之列表[通俗易懂]
Understanding of NAND flash deblocking