当前位置:网站首页>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.
边栏推荐
- 队列的链式存储
- 高薪程序员&面试题精讲系列118之Session共享有哪些方案?
- Wechat simulated geographical location_ Camouflage wechat location
- Scene function of wooden frame
- Huawei interview question: Recruitment
- Share several tools for designing exquisite circuit diagrams
- Accept different views with an open mind
- 类的初始化与实例化
- 6.30模拟赛总结
- How to install php7 and perform performance test using yum
猜你喜欢

"Analysis of 43 cases of MATLAB neural network": Chapter 40 research on prediction of dynamic neural network time series -- implementation of NARX based on MATLAB

Double linked list related operations

I wish you all a happy reunion

redis探索之缓存击穿、缓存雪崩、缓存穿透

【20211129】Jupyter Notebook遠程服務器配置
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service

2022-06-28-06-29
![[encounter Django] - (II) database configuration](/img/13/9512c1e03349092874055771c3433d.png)
[encounter Django] - (II) database configuration

Queue operation---

手机便签应用
随机推荐
MySQL workbench data modeling function
手机便签应用
Four years after graduation: work, resign, get married, buy a house
Scene function of wooden frame
使用BurpSuite对app抓包教程
codeforces -- 4B. Before an Exam
[20211129] configuration du serveur distant du carnet de notes jupyter
Relationship between accuracy factor (DOP) and covariance in GPS data (reference link)
Simple Fibonacci (recursive)
ustime写出了bug
Understanding of NAND flash deblocking
leetcode:241. 为运算表达式设计优先级【dfs + eval】
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
项目部署,一点也不难!
A hole in solder paste
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
IOS interview
哪个券商公司开户佣金低又安全又可靠
第十四章 信号(四)- 多进程任务示例
Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.