Algorithm interview question: how many 7 are there between 1 and 1000?

Time：2021-8-25

I saw a very interesting interview question these two days: the examiner directly asked, 1 to 1000 to how many 7?

Requirements, no programming, give the answer directly, and give a simple idea.

C / C + + Learning skirt [712]   two eight four   seven zero five  】， Whether you are Xiaobai or an advanced person, whether you want to change careers or enter a career, you can come to understand, progress and learn together! There are development tools in the skirt, and many dry goods and technical data are shared!

The first idea

First of all, there should be a reasonable classification. From the beginning, I thought of a reasonable classification, that is, from 1 to 1000, each number is regarded as three digits, while 1000 obviously has no 7. Regardless of the 1, it is regarded as 001, 19 as 019, and so on. In this way, each number can be represented by three grids, and there is a unified representation method:

Mouth mouth mouth

In the first step, only the last two grids are considered. At first, I only thought about the first case, X7, i.e. 07, 17, up to 97, without considering the particularity of 77 (the idea of isolation), so there are 10 7 from 0 to 9, and then consider 77, there are 11 7. There is another case, 7x, that is, 70, 71, up to 79. The situation is the same as above, and there are also 11 7.

In both cases, two 7’s in 77 are counted, so subtract 2. The result is 22-2 = 20.

Second, consider the first lattice. In the first lattice, from 0 to 9, there are 10 above situations, of which 7 is special. Let’s not treat it as 7 (isolated thought), so the situation is simple. There are 10 * 20 = 200 7 in total.

The third step is to consider the 7 that have just been isolated. This step is easy to think crooked. Do you think it is + 20? In fact, you should think carefully. 701, 719 and 722 have one more 7. What about 777? Think about it carefully. The last two 7S in the 777 have also been calculated in front. That’s clear. The isolation just now only ignores the case that there is a 7 in front of the 100 numbers from 00 to 99.

So the final answer is 200 + 100 = 300.

It is assumed that the previous results are represented by F (3).

It is not difficult to conclude that 1 to 10000, that is, f (4) = 10 * f (3) + 1000, that is, 4000

Another idea

How many 7’s are there in the question, and how many 1’s, or 2’s, or 9’s are there in the question? It is not difficult to guess 1~9 that the situation is the same. Ignore one more 1 in 1000 first.

Is it possible to find out how many zeros there are, and then find the total number of characters of 1 ~ 1000 numbers, subtract the number of zeros, and then divide by 9?

Step 1: find the total number of characters of 1 ~ 1000 numbers

1 digit, 9

2 digits, 90, 2 = 180 (99 from 1 to 99, minus 9)

3 digits, 900, 3 = 2700 (similar to 10 ~ 99 above, 100 ~ 999 here)

4 digits, 1 * 4 = 4

The total number is 2700 + 180 + 9 + 4 = 2893 characters

Step 2: find out how many zeros there are

1 digit, no

2 digits. Only x0 is considered. There are 9 digits from 10 to 99

3 digits. Consider 11 cases of 0x and x0 respectively, minus 2 duplicates, i.e. 211-2 = 20. There are 9 cases from 100 to 999, i.e. 920 = 180

4 digits, 3 zeros

The result is 2700 + 180 + 9 + 4 – 180 + 9-3 = 2701, so subtract the 1 in 1000, which is exactly 2700.

The result is easy. Regardless of 1, 1 ~ 9 occur 2700 / 9 = 300 times.

This solution is indirect, which is more troublesome than direct.

summary

Representation and classification are very important. Isolation method (one of the best methods in high school physics) can simplify problems and facilitate analysis!

Learn to use global parameters and directory parameters in apipost in one minute

Global and catalog parameters In the previous example, we filled in different request header, query and body parameters in a single interface. However, in actual projects, a batch of interfaces often have the same request parameters. At this point, we can use global parameters or directory parameters. For example, for common token parameters, if we […]