Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write code to realize the number of operations that turn a number into zero

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to write code to realize the number of operations that turn a number into zero". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to write code to realize the number of operations that turn a number into zero".

Problem description

Give you a non-negative integer num and ask you to return the number of steps required to turn it into zero. If the current number is even, you need to divide it by 2; otherwise, subtract 1.

To make it easier to understand the meaning of the topic, here are two examples:

Example 1:

Enter: num = 14

Output: 6

Explanation:

Step 1) 14 is an even number, divided by 2 to get 7.

Step 2) 7 is odd, minus 1 to get 6.

Step 3) 6 is an even number, divided by 2 to get 3.

Step 4) 3 is odd, minus 1 to get 2.

Step 5) 2 is an even number, divided by 2 to get 1.

Step 6) 1 is odd, minus 1 to get 0.

Example 2:

Enter: num = 8

Output: 4

Explanation:

Step 1) 8 is an even number, divided by 2 to get 4.

Step 2) 4 is an even number, divided by 2 to get 2.

Step 3) 2 is an even number, divided by 2 to get 1.

Step 4) 1 is odd, minus 1 to get 0.

(source: force buckle)

Solution

First of all, it can be known from the meaning of the topic: the topic requires that a non-negative integer num be given, return the number of times it needs to be changed to zero, and that when the number is even, it needs to be divided by 2, and when it is odd, subtract 1. The operation is repeated until it is changed to 0, and the number of times the operation is returned.

Then, let's find out the key points: first, num is a non-negative integer, so our code does not need to judge the type of logarithm, and second, when the number is large enough, it will definitely go through many operations of subtracting 1 or dividing by 2, then it will definitely use a loop. If we think about it further, the commonly used cycles are mainly for cycle and while cycle, so which one is better to solve this problem? So we further consider that the for loop is generally used to operate within the target number of cycles when the number of cycles is known, while the while loop is generally used to cycle within the range of conditions without knowing the number of cycles, and exit the loop directly after meeting the conditions. It can be seen that after comparing the two cycles, it is more appropriate to choose the while cycle for this problem.

Finally, after determining the loop, do not rush to write code, the title also requires that when the number becomes even, it needs to be divided by 2, and when it is odd, it needs to be subtracted by 1. So, in the while loop, we also need to add a judgment statement. Only in this way can we finish the problem within the scope of the question.

Maybe you have a question now. For loops operate when you know the number of loops, so it's easy to count the number of loops. But if while loops jump out of loops when they meet the conditions, how to count the number of loops? This problem is actually very simple, we just need to define a variable with an initial value of 0 outside the loop, and then add 1 to each loop, so that after the loop is completed, the number of loops will be counted!

Please take a look at the problem solving code below:

Def math ():

Num = int (input ('))

A = 0

While num! = 0:

If num% 2 = = 0:

Num = num / 2

A = a + 1

Elif num% 2! = 0:

Num = num-1

A = a + 1

Return a

Print (math ())

Test results:

The above code is suitable for the ideas of most friends. I have also found other ideas in other places. Now I would like to share the code with you:

Class Solution:

Def numberOfSteps (self, num: int)-> int:

Return bin (num) .count ('1') + num.bit_length ()-1

At this point, I believe you have a deeper understanding of "how to write code to achieve the number of operations that turn a number into zero". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report